Created
March 9, 2019 08:18
-
-
Save Airr/d3b79a9fa6b67dd745e8744b096ffdfc to your computer and use it in GitHub Desktop.
Public IP - Using Go "net" package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"net" | |
) | |
func main() { | |
resolver := net.Resolver{ | |
PreferGo: true, | |
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { | |
d := net.Dialer{} | |
return d.DialContext(ctx, "udp", "resolver1.opendns.com:53") | |
}, | |
} | |
ips, err := resolver.LookupHost(context.Background(), "myip.opendns.com") | |
if err != nil { | |
log.Fatal(err) | |
} | |
for _, ip := range ips { | |
fmt.Println(ip) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment