pkg/netutil/routes_linux.go
func GetDefaultHost() (string, error) {
	rmsgs, rerr := getDefaultRoutes()
	if rerr != nil {
		return "", rerr
	}

	// prioritize IPv4
	if rmsg, ok := rmsgs[syscall.AF_INET]; ok {
		if host, err := chooseHost(syscall.AF_INET, rmsg); host != "" || err != nil {
			return host, err
		}
		delete(rmsgs, syscall.AF_INET)
	}

	// sort so choice is deterministic
	var families []int
	for family := range rmsgs {
		families = append(families, int(family))
	}
	sort.Ints(families)

	for _, f := range families {
		family := uint8(f)
		if host, err := chooseHost(family, rmsgs[family]); host != "" || err != nil {
			return host, err
		}
	}

	return "", errNoDefaultHost
}
func GetDefaultHost() (string, error) {
	rmsgs, rerr := getDefaultRoutes()
	if rerr != nil {
		return "", rerr
	}

	// prioritize IPv4
	if rmsg, ok := rmsgs[syscall.AF_INET]; ok {
		if host, err := chooseHost(syscall.AF_INET, rmsg); host != "" || err != nil {
			return host, err
		}
		delete(rmsgs, syscall.AF_INET)
	}

	// sort so choice is deterministic
	var families []int
	for family := range rmsgs {
		families = append(families, int(family))
	}
	sort.Ints(families)

	for _, f := range families {
		family := uint8(f)
		if host, err := chooseHost(family, rmsgs[family]); host != "" || err != nil {
			return host, err
		}
	}

	return "", errNoDefaultHost
}
func GetDefaultHost ( ) ( string , error ) { rmsgs , rerr := getDefaultRoutes ( ) if rerr != nil { return " " , rerr } // prioritize IPv4 if rmsg , ok := rmsgs [ syscall . AF_INET ] ; ok { if host , err := chooseHost ( syscall . AF_INET , rmsg ) ; host != " " || err != nil { return host , err } delete ( rmsgs , syscall . AF_INET ) } // sort so choice is deterministic var families [ ] int for family := range rmsgs { families = append ( families , int ( family ) ) } sort . Ints ( families ) for _ , f := range families { family := uint8 ( f ) if host , err := chooseHost ( family , rmsgs [ family ] ) ; host != " " || err != nil { return host , err } } return " " , errNoDefaultHost }
// GetDefaultHost obtains the first IP address of machine from the routing table and returns the IP address as string.
// An IPv4 address is preferred to an IPv6 address for backward compatibility.
GetDefaultHost obtains the first IP address of machine from the routing table and returns the IP address as string . An IPv4 address is preferred to an IPv6 address for backward compatibility .