Last active
June 25, 2020 08:01
-
-
Save rmoff/bdf119be932186fca146b899d3b9f505 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
go version go1.13.7 darwin/amd64 |
This file contains hidden or 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
len=6 cap=6 array ptr: 0xc00000c080 value: [2 3 5 7 11 13] | |
len=0 cap=6 array ptr: 0xc00000c0c0 value: [] | |
len=4 cap=6 array ptr: 0xc00000c100 value: [2 3 5 7] | |
len=2 cap=4 array ptr: 0xc00000c140 value: [5 7] |
This file contains hidden or 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" | |
"unsafe" | |
) | |
func main() { | |
s := []int{2, 3, 5, 7, 11, 13} | |
printSlice(s) | |
// Slice the slice to give it zero length. | |
s = s[:0] | |
printSlice(s) | |
// Extend its length. | |
s = s[:4] | |
printSlice(s) | |
// Drop its first two values. | |
s = s[2:] | |
printSlice(s) | |
} | |
func printSlice(s []int) { | |
fmt.Printf("len=%d\tcap=%d\tarray ptr: %v\tvalue: %v\n", len(s), cap(s), (*unsafe.Pointer)(unsafe.Pointer(&s)), s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment