I am building some test code and I try to compare pointers with the <
, >
operators. My goal is to order elements into a balanced tree structure (implemented by me). For that, I need quick, generic comparation. Following the good old C/C++ customs, my current idea is (...was, at the time of writing this post) to compare them by their pointer addresses.
However, as I've found, it doesn't work. The source
if (&(a.val) < &(b.val)) {
gives the compile error
./pointer.go:40: invalid operation: &a.val < &b.val (operator < not defined on pointer)
a
and b
are structs with val
as (safe) pointer member.
Is it possible to compare safe pointers in go? How?
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.)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…