In Go one can send UDP packets using net.Addr
interface to specify the target endpoint. Some special addresses, e.g. :8080
and 0.0.0.0
, sends packets using local loopback interface. When received, still on the same host, the message's net.Addr
shows [::1]:8080
as source. What is the the easiest way to determine that the packet was sent and received by the same host?
Here's an example in the Go Playground. It shows 0.0.0.0:8080
(ipv4) instead of [::1]:8080
.
:8080
is not an address, it is a port number, typically used when testing your own http websites on Windows because on Windows you cannot easily use port 80, the actual http port. This will only use the loopback interface if you use localhost as the IP address.The localhost address for IPv4 is
127.0.0.1
and for IPv6 it is::1
. The address0.0.0.0
is usually used as a placeholder address to indicate, for example, listening on all IP addresses, see this question.As mentioned in the comment, you can use net.IP.Equal to check if your peer is localhost. Just compare your address in question to
127.0.0.1
or to::1
, thenet.IP.Equal
function considers them equal.