github.com/zly-app/zapp@v1.3.3/pkg/utils/get_instance.go (about)

     1  package utils
     2  
     3  import (
     4  	"net"
     5  )
     6  
     7  // 获取实例名, 会返回本地ip, 如果无法获取返回默认值
     8  func GetInstance(def string) string {
     9  	var ips []string
    10  	address, _ := net.InterfaceAddrs()
    11  
    12  	for _, addr := range address {
    13  		if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
    14  			if ipNet.IP.To4() != nil {
    15  				ips = append(ips, ipNet.IP.String())
    16  			}
    17  		}
    18  	}
    19  
    20  	if len(ips) > 0 {
    21  		return ips[0]
    22  	}
    23  	return def
    24  }