Skip to content

Instantly share code, notes, and snippets.

@saiday
Created February 11, 2018 12:41
Show Gist options
  • Save saiday/c64bd4ae19fd3a542038a14b991ded54 to your computer and use it in GitHub Desktop.
Save saiday/c64bd4ae19fd3a542038a14b991ded54 to your computer and use it in GitHub Desktop.
Golang remove first differences between slice value and pointer
package main
import "fmt"
func main() {
s := []int{2, 3, 5, 7, 11, 13}
printSlice(s)
removeFirst(s)
printSlice(s)
removeFirstAddress(&s)
printSlice(s)
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
func removeFirst(s []int) {
s = append(s[:0], s[1:]...)
}
func removeFirstAddress(s *[]int) {
*s = append((*s)[:0], (*s)[1:]...)
}
// output:
// len=6 cap=6 [2 3 5 7 11 13]
// len=6 cap=6 [3 5 7 11 13 13]
// len=5 cap=6 [5 7 11 13 13]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment