// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package util import ( "errors" "net" ) // LocalIP() 获取本机IP // 返回结果:IPv6 或 IPv4 func LocalIP() (string, error) { addrs, err := net.InterfaceAddrs() if err != nil { return "", err } for _, addr := range addrs { if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { if ipnet.IP.To4() != nil { return ipnet.IP.String(), nil } } } return "", errors.New("get ip address failed.") }