Skip to content

Instantly share code, notes, and snippets.

@ankanch
Created October 25, 2017 13:13
Show Gist options
  • Save ankanch/8c8ec5aaf374039504946e7e2b2cdf7f to your computer and use it in GitHub Desktop.
Save ankanch/8c8ec5aaf374039504946e7e2b2cdf7f 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)
}
@TotallyNotAHaxxer
Copy link

fucking finally, thanks LOL it took me hours to troubleshoot older scripts ive seen- but now i feel dumb for not thinking of this XDDD

@Runinterface
Copy link

Runinterface commented Apr 2, 2022

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment