Created
February 11, 2018 12:41
-
-
Save saiday/c64bd4ae19fd3a542038a14b991ded54 to your computer and use it in GitHub Desktop.
Golang remove first differences between slice value and pointer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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