123456789101112131415161718192021222324252627 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.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.")
- }
|