Created
August 3, 2021 06:11
-
-
Save jinagamvasubabu/2e18b20569e7075c066d2c858da7d383 to your computer and use it in GitHub Desktop.
slice_3.go
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" | |
| "reflect" | |
| "unsafe" | |
| ) | |
| func main() { | |
| a := []int{1, 2, 3, 4, 5} | |
| b := a[2:] | |
| fmt.Printf("a: %v, sliceHeader: %v\n", a, getSliceHeader(&a)) | |
| fmt.Printf("b: %v, sliceHeader: %v\n", b, getSliceHeader(&b)) | |
| b = append(b, 20) | |
| fmt.Printf("a: %v, sliceHeader: %v\n", a, getSliceHeader(&a)) | |
| fmt.Printf("b: %v, sliceHeader: %v\n", b, getSliceHeader(&b)) | |
| } | |
| // https://stackoverflow.com/a/54196005/463785 | |
| func getSliceHeader(slice *[]int) string { | |
| sh := (*reflect.SliceHeader)(unsafe.Pointer(slice)) | |
| return fmt.Sprintf("%+v", sh) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment