Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

memory

go

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?

like image 909
Timur Fayzrakhmanov Avatar asked Nov 11 '14 07:11

Timur Fayzrakhmanov


1 Answers

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:

  • "How do I print the pointer value of a Go object? What does the pointer value mean?".
  • "Go by Example: String Formatting".

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

like image 185
VonC Avatar answered Nov 17 '22 10:11

VonC