how to print the memory address of a slice and that of its underlying array

tags: learning go programming

content

slice := []int{1, 2, 3}
fmt.Printf("type of slice: %T", slice)  
// prints []int, slice is NOT a pointer
fmt.Printf("memory addr of slice: %p", &slice)
// prints memory address of slice
fmt.Printf("memory addr of underlying array: %p", slice)
// prints memory address of underlying array
fmt.Println("memory addr of underlying array:", &slice[0])
  • %p is a special thing.
    • s is a slice, not a pointer
    • but when a slice is passed to %p, fmt prints out the memory address of its underlying array
  • %p, slice and &slice[0] are literally the same
    • %p, &slice[0] is the same too!

up

down

reference