Created
October 25, 2017 13:13
-
-
Save ankanch/8c8ec5aaf374039504946e7e2b2cdf7f to your computer and use it in GitHub Desktop.
[Golang] Get Public IP address via Public IP API
This file contains 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
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