Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: Addr and Interface documentation is not clear #31276

Open
AlekSi opened this issue Apr 5, 2019 · 1 comment

Comments

@AlekSi
Copy link
Contributor

commented Apr 5, 2019

What version of Go are you using (go version)?

go version go1.12.1 darwin/amd64

I also checked older versions – they have the same problem.

What did you do?

package main

import (
    "log"
    "net"
)

func main() {
    ifaces, _ := net.Interfaces()
    for _, iface := range ifaces {
        addrs, _ := iface.Addrs()
        for _, addr := range addrs {
            log.Print(addr.String())
        }
    }
}

What did you expect to see?

Addresses without network mask.

type Addr interface {
        Network() string // name of the network (for example, "tcp", "udp")
        String() string  // string form of address (for example, "192.0.2.1:25", "[2001:db8::1]:80")
}

What did you see instead?

Addresses with network mask.

2019/04/05 14:12:06 127.0.0.1/8
2019/04/05 14:12:06 ::1/128
2019/04/05 14:12:06 fe80::1/64
2019/04/05 14:12:06 192.168.1.35/24
…

I think net.Addr and net.Interface.Addrs() documentation should mention that. Probably the best way is to say that it returns *IPNets.

@mennis

This comment has been minimized.

Copy link

commented Apr 26, 2019

Addr is more general than local network interfaces as indicated by the comment above it:

// The two methods Network and String conventionally return strings
// that can be passed as the arguments to Dial, but the exact form
// and meaning of the strings is up to the implementation.

and net.Interface.Addrs() is more general than IP .

net.Interface.Addrs() does return []Addr. In this case the type of Addr the system returns is IPNet. I would have also found this to be surprising (that I need to call ParseCIDR on addr.String() to get the IP portion); however, if a local interface has an IP address IPNet is more complete as the mask is important to making decisions about broadcasting and routing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.