You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 viikko sitten
12345678910111213141516171819202122
  1. package util
  2. import (
  3. "errors"
  4. "net"
  5. )
  6. func LocalIP() (string, error) {
  7. addrs, err := net.InterfaceAddrs()
  8. if err != nil {
  9. return "", err
  10. }
  11. for _, address := range addrs {
  12. if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
  13. if ipnet.IP.To4() != nil {
  14. return ipnet.IP.String(), nil
  15. }
  16. }
  17. }
  18. return "", errors.New("not found")
  19. }