3 ms·
Ah, thanks for pointing this out. You're entirely correct. I'm not sure how I got confused with the special case around string comparisons, and forgot that it
by coder543 6y ago
Ah, thanks for pointing this out. You're entirely correct.
I'm not sure how I got confused with the special case around string comparisons, and forgot that it didn't apply to regular struct pointers. It was late in the evening, so I guess that's my excuse?
BenchmarkSameRef-12 620150179 1.92 ns/op
BenchmarkDiffRef-12 6102709 204 ns/op
Source: https://play.golang.org/p/DbjkstOCZ-Y https://play.golang.org/p/DbjkstOCZ-Y (although you can't actually run it on the Go playground)
Go will check whether two strings have the same underlying pointer first. If the pointers are different, then it compares the contents.
I stand by my earlier statement that I think pure reference equality is just a bad idea 99.9% of the time. I would prefer if Go did deep equality with every comparison when needed the way it does with strings. If you have to use some clunky syntax or built-in function to do just a reference comparison, that would be perfectly fine with me. Unfortunately, Go does exactly what you described.
With a quick google, I didn't immediately find a linter to complain about comparing struct pointers rather than struct values, so maybe I'll write a linter like that sometime if I can find the time and motivation.