Sure, you can do that using the unsafe package:
https://play.golang.org/p/Wd7hWn9Zsu
package main
import (
"fmt"
"strconv"
"unsafe"
)
func main() {
//Given:
data := "Hello"
ptrString := fmt.Sprintf("%d", &data)
//Convert it to a uint64
ptrInt, _ := strconv.ParseUint(ptrString, 10, 64)
//They should match
fmt.Printf("Address as String: %s as Int: %d
", ptrString, ptrInt)
//Convert the integer to a uintptr type
ptrVal := uintptr(ptrInt)
//Convert the uintptr to a Pointer type
ptr := unsafe.Pointer(ptrVal)
//Get the string pointer by address
stringPtr := (*string)(ptr)
//Get the value at that pointer
newData := *stringPtr
//Got it:
fmt.Println(newData)
//Test
if(stringPtr == &data && data == newData) {
fmt.Println("successful round trip!")
} else {
fmt.Println("uhoh! Something went wrong...")
}
}
However, keep in mind the various warnings on the unsafe package. For example:
"A uintptr is an integer, not a reference. Converting a Pointer to a uintptr creates an integer value with no pointer semantics. Even if a uintptr holds the address of some object, the garbage collector will not update that uintptr's value if the object moves, nor will that uintptr keep the object from being reclaimed." - https://golang.org/pkg/unsafe/#Pointer