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

AlekSi opened this issue Apr 5, 2019 · 1 comment

net: Addr and Interface documentation is not clear #31276

AlekSi opened this issue Apr 5, 2019 · 1 comment
Documentation NeedsInvestigation


Copy link

@AlekSi AlekSi 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 (

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

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, "", "[2001:db8::1]:80")

What did you see instead?

Addresses with network mask.

2019/04/05 14:12:06
2019/04/05 14:12:06 ::1/128
2019/04/05 14:12:06 fe80::1/64
2019/04/05 14:12:06

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

@bcmills bcmills added the NeedsInvestigation label Apr 10, 2019
Copy link

@mennis mennis 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
Documentation NeedsInvestigation
None yet

No branches or pull requests

4 participants