package ip

import (
	"im-backend/utils/logs"
	"net"
)

func GetLocalIP() string {
	addrs, err := net.InterfaceAddrs()
	logs.MyLogger.Infoln("addrs: ", addrs)
	if err != nil {
		logs.MyLogger.Errorf("无法获取本机IP地址:%s", err)
		return ""
	}
	for _, addr := range addrs {
		if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
			resIp := ipNet.IP.To4()
			if resIp != nil {
				return resIp.String()
			}
		}
	}
	return ""
}