问题描述

我有一定的C经验,并且对golang完全陌生.

I have some experience in C and I am totally new to golang.

func learnArraySlice() {
  intarr := [5]int{12, 34, 55, 66, 43}
  slice := intarr[:]
  fmt.Printf("the len is %d and cap is %d \n", len(slice), cap(slice))
  fmt.Printf("address of slice 0x%x add of Arr 0x%x \n", &slice, &intarr)
}

现在golang slice中是array的引用,其中包含指向slice的数组len和slice的cap的指针,但是该slice也将分配在内存中,我想打印该内存的地址.但是无法做到这一点.

Now in golang slice is a reference of array which contains the pointer to an array len of slice and cap of slice but this slice will also be allocated in memory and i want to print the address of that memory. But unable to do that.

推荐答案

fmt.Printf("address of slice %p add of Arr %p \n", &slice, &intarr)
%p

这篇关于如何在Golang中打印切片的内存地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!