Skip to content

Instantly share code, notes, and snippets.

@mavencode01
Forked from ankanch/getlocalIP.go
Created January 17, 2019 10:15
Show Gist options
  • Save mavencode01/f9cb08ddc42633f9b19f9a655bc41417 to your computer and use it in GitHub Desktop.
Save mavencode01/f9cb08ddc42633f9b19f9a655bc41417 to your computer and use it in GitHub Desktop.
[Golang] Get Public IP address via Public IP API
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
url := "https://api.ipify.org?format=text" // we are using a pulib IP API, we're using ipify here, below are some others
// https://www.ipify.org
// http://myexternalip.com
// http://api.ident.me
// http://whatismyipaddress.com/api
fmt.Printf("Getting IP address from ipify ...\n")
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
ip, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Printf("My IP is:%s\n", ip)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment