Home >Backend Development >Golang >Why can't I compare net.Addr?

Why can't I compare net.Addr?

王林
王林forward
2024-02-12 14:36:05750browse

为什么我无法比较 net.Addr?

Question content

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

Workaround

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete