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

Why can't I compare net.Addr?

王林
Release: 2024-02-12 14:36:05
forward
760 people have browsed it

为什么我无法比较 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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template