如何将内存地址(例如0xc20803a000)转换为字符串?使用Golang可以这样做:

huangapple go评论72阅读模式
英文:

How to convert memory address (like 0xc20803a000) to string? Golang

问题

我认为问题已经很清楚了。我想知道如何将像0xc20803a000这样的内存地址转换为字符串类型。这可能吗?

英文:

I think question is quite self-explained. I want to know how can I convert memory address like this 0xc20803a000 to string type. Is it possible?

答案1

得分: 15

你可以使用fmt.Sprintf()函数,其中使用%p来表示指针的十六进制表示(带有前缀0x)。

示例代码如下:

myString := fmt.Sprintf("%p", yourPointer)

fmt.Sprintf()函数返回一个字符串。

你可以在以下链接中找到几个打印内存指针的示例:

在这些示例中,将Printf替换为Sprintf,你就可以得到一个字符串来使用了。

英文:

You can use fmt.Sprintf(), with %p (base 16 notation, with leading 0x)

myString := fmt.Sprintf("%p", yourPointer)

fmt.Sprintf() returns a string.

You can see several examples (of printing a memory pointer) in:

Replace in those examples Printf by Sprintf, and you have a string for you to use.

huangapple
  • 本文由 发表于 2014年11月11日 15:40:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/26859877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定