Home >Backend Development >Golang >Why can't I compare net.Addr?
I'm trying to understand/understand why I can't compare two identical net.Addr.
From this post, it seems like two interfaces should be able to compare if they have the same underlying type and that type can be compared. In the following example, the underlying type is net.UDPAddr. It is a structure containing string, int and net.IP, which is a type alias of []byte.
Is it because sliced IPs are not comparable? Even if it's the same type, length and content?
Example:
https://go.dev/play/p/dIzRCTwBA4P
Your example compares two callsnet.ResolveUDPAddr
Returned value. However, if you consider the function signature:
You'll notice that you are actually comparing two pointers; they are comparable, but not equal (you are comparing the pointer; not the value it points to).
According to go specification:
net.UDPAddr contains a net.IP
which is a []byte
and is therefore not comparable.
This example extension may be helpful.
The above is the detailed content of Why can't I compare net.Addr?. For more information, please follow other related articles on the PHP Chinese website!