Skip to content

Instantly share code, notes, and snippets.

@jinagamvasubabu
Created August 3, 2021 06:11
Show Gist options
  • Select an option

  • Save jinagamvasubabu/2e18b20569e7075c066d2c858da7d383 to your computer and use it in GitHub Desktop.

Select an option

Save jinagamvasubabu/2e18b20569e7075c066d2c858da7d383 to your computer and use it in GitHub Desktop.
slice_3.go
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