-
-
Save mavencode01/f9cb08ddc42633f9b19f9a655bc41417 to your computer and use it in GitHub Desktop.
[Golang] Get Public IP address via Public IP API
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 ( | |
"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