Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

How can I compare pointers in Go?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...