Skip to content

Instantly share code, notes, and snippets.

@lysu
Created July 18, 2018 16:46
Show Gist options
  • Save lysu/c18d9a9f7816bf4fe651e97b91f3fa92 to your computer and use it in GitHub Desktop.
Save lysu/c18d9a9f7816bf4fe651e97b91f3fa92 to your computer and use it in GitHub Desktop.
func getLocalAddr(machines []string) (string, error) {
for _, m := range machines {
conn, err := net.Dial("udp", m)
if err != nil {
continue
}
defer conn.Close()
return strings.Split(conn.LocalAddr().String(), ":")[0], nil
}
return "", errors.New("no reachable machine to get local addr")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment