package main import ( "fmt" "log" "net" "strings" ) type Network struct { Name string IP string MACAddress string } type intfInfo struct { Name string MacAddress string Ipv4 []string } func getNetworkInfo() error { intf, err := net.Interfaces() if err != nil { log.Fatal("get network info failed: %v", err) return err } var is = make([]intfInfo, len(intf)) for i, v := range intf { ips, err := v.Addrs() if err != nil { log.Fatal("get network addr failed: %v", err) return err } //此处过滤loopback(本地回环)和isatap(isatap隧道) if !strings.Contains(v.Name, "Loopback") && !strings.Contains(v.Name, "isatap") { var network Network is[i].Name = v.Name is[i].MacAddress = v.HardwareAddr.String() for _, ip := range ips { if strings.Contains(ip.String(), ".") { is[i].Ipv4 = append(is[i].Ipv4, ip.String()) } } network.Name = is[i].Name network.MACAddress = is[i].MacAddress if len(is[i].Ipv4) > 0 { network.IP = is[i].Ipv4[0] } fmt.Printf("network:=", network) } } return nil } func main() { getNetworkInfo() }