func IP() (ip string, err error) {

	var interfaces []net.Interface
	if interfaces, err = net.Interfaces(); err != nil {
		return "", fmt.Errorf("Failed to load network interfaces. %v", err)
	}

	interfaces = filterInterface(interfaces)
	sort.Sort(byIndex(interfaces))

	var foundIP net.IP

	// search for IPv4
	for _, i := range interfaces {
		var addrs []net.Addr
		if addrs, err = i.Addrs(); err != nil {
			continue
		}
		for _, addr := range addrs {
			switch v := addr.(type) {
			case *net.IPAddr:
				foundIP = v.IP.To4()
			case *net.IPNet:
				foundIP = v.IP.To4()
			}

			if foundIP != nil {
				return foundIP.String(), nil
			}
		}
	}

	// search for IPv6
	for _, i := range interfaces {
		var addrs []net.Addr
		if addrs, err = i.Addrs(); err != nil {
			continue
		}
		for _, addr := range addrs {
			switch v := addr.(type) {
			case *net.IPAddr:
				foundIP = v.IP.To16()
			case *net.IPNet:
				foundIP = v.IP.To16()
			}

			if foundIP != nil {
				return foundIP.String(), nil
			}
		}
	}

	return "", fmt.Errorf("No IP addresses found.")
}
// IP of the network interface
IP of the network interface
// IP of the network interface
func IP() (ip string, err error) {
	var interfaces []net.Interface
	if interfaces, err = net.Interfaces(); err != nil {
		return "", fmt.Errorf("Failed to load network interfaces. %v", err)
	}
	interfaces = filterInterface(interfaces)
	sort.Sort(byIndex(interfaces))
	var foundIP net.IP
	for _, i := range interfaces {
		var addrs []net.Addr
		if addrs, err = i.Addrs(); err != nil {
			continue
		}
		for _, addr := range addrs {
			switch v := addr.(type) {
			case *net.IPAddr:
				foundIP = v.IP.To4()
			case *net.IPNet:
				foundIP = v.IP.To4()
			}
			if foundIP != nil {
				return foundIP.String(), nil
			}
		}
	}
	for _, i := range interfaces {
		var addrs []net.Addr
		if addrs, err = i.Addrs(); err != nil {
			continue
		}
		for _, addr := range addrs {
			switch v := addr.(type) {
			case *net.IPAddr:
				foundIP = v.IP.To16()
			case *net.IPNet:
				foundIP = v.IP.To16()
			}
			if foundIP != nil {
				return foundIP.String(), nil
			}
		}
	}
	return "", fmt.Errorf("No IP addresses found.")
}
func IP ( ) ( ip string , err error ) { var interfaces [ ] net . Interface if interfaces , err = net . Interfaces ( ) ; err != nil { return " " , fmt . Errorf ( " " , err ) } interfaces = filterInterface ( interfaces ) sort . Sort ( byIndex ( interfaces ) ) var foundIP net . IP for _ , i := range interfaces { var addrs [ ] net . Addr if addrs , err = i . Addrs ( ) ; err != nil { continue } for _ , addr := range addrs { switch v := addr . ( type ) { case * net . IPAddr : foundIP = v . IP . To4 ( ) case * net . IPNet : foundIP = v . IP . To4 ( ) } if foundIP != nil { return foundIP . String ( ) , nil } } } for _ , i := range interfaces { var addrs [ ] net . Addr if addrs , err = i . Addrs ( ) ; err != nil { continue } for _ , addr := range addrs { switch v := addr . ( type ) { case * net . IPAddr : foundIP = v . IP . To16 ( ) case * net . IPNet : foundIP = v . IP . To16 ( ) } if foundIP != nil { return foundIP . String ( ) , nil } } } return " " , fmt . Errorf ( " " ) }
IP of the network interface
IP of the network interface
// search for IPv4 // search for IPv6