1. 获取本机 IP 地址
package mainimport ("fmt""net"
)// 获取本机网卡IP
func getLocalIP() (ipv4 string, err error) {// var (// addrs []net.Addr// addr net.Addr// ipNet *net.IPNet // IP地址// isIpNet bool// )// 获取所有网卡addrs, err := net.InterfaceAddrs()if err != nil {return}// 取第一个非lo的网卡IPfor _, addr := range addrs {// 这个网络地址是IP地址: ipv4, ipv6ipNet, isIpNet := addr.(*net.IPNet)if isIpNet && !ipNet.IP.IsLoopback() {// 跳过IPV6if ipNet.IP.To4() != nil {ipv4 = ipNet.IP.String() // 192.168.1.1return}}}return
}func main() {ipv4, _ := getLocalIP()fmt.Printf("ipv4 is <%s>", ipv4)
}