Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...