[Solved] How can I compare pointers in Go?


Is it guaranteed that the order of the pointers remains the same? (For
example, I can imagine some GC trickery which also reorders the data.)

In answer to this part of your question, it doesn’t look like it. See pkg unsafe linked below.

If you want the memory address of a pointer, try the unsafe package:

https://golang.org/pkg/unsafe/#Pointer

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    var a,b int
    var pa,pb *int
    pa = &a
    pb = &b

    //var c int
    //pa = &c

    if uintptr(unsafe.Pointer(pa)) < uintptr(unsafe.Pointer(pb)) {
      fmt.Printf("a:%p > b:%p",pa,pb)   
    } else {
      fmt.Printf("b:%p > a:%p",pb,pa)
    }

}

This will let you get an arbitrary Pointer type and then the current memory address of that pointer (as printf would). Note the caveats there though, you cannot rely on this address:

Converting a Pointer to a uintptr produces the memory address of the
value pointed at, as an integer. The usual use for such a uintptr is
to print it. Conversion of a uintptr back to Pointer is not valid in
general. 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.

This bypasses the Go type system and memory security so it’s unsafe and you probably don’t want to do it, unless you’re just experimenting. I can’t think of a reason to do it in tests, but if you want to be sure to read that entire document and be very sure you know that what you are doing will work as you expect.

1

solved How can I compare pointers in Go?