如何在Golang中打印切片的内存地址? [英] How to print the memory address of a slice in Golang?

查看:2789
本文介绍了如何在Golang中打印切片的内存地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一定的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.

推荐答案

http://golang.org/pkg/fmt/

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

%p将打印地址.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆