util.go 426 B

1234567891011121314151617181920212223
  1. package common
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. // GetLocalIP 获取本地内网IP地址
  7. func GetLocalIP() (string, error) {
  8. adders, err := net.InterfaceAddrs()
  9. if err != nil {
  10. return "", err
  11. }
  12. for _, addr := range adders {
  13. if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  14. if ipnet.IP.To4() != nil {
  15. return ipnet.IP.String(), nil
  16. }
  17. }
  18. }
  19. return "", fmt.Errorf("unable to determine local ip")
  20. }