From a63f9d657e953aba2e6e4447d14da1b50b09a00f Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Wed, 16 Nov 2016 19:19:41 -0800 Subject: [PATCH] Get and print IP address from https://whatismyipv6.buddy.wtf/json --- .gitignore | 3 ++- main.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 main.go diff --git a/.gitignore b/.gitignore index d3beee5..55f4dc3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +update-dns + # ---> Go # Compiled Object files, Static and Dynamic libs (Shared Objects) *.o @@ -23,4 +25,3 @@ _testmain.go *.exe *.test *.prof - diff --git a/main.go b/main.go new file mode 100644 index 0000000..9570dc4 --- /dev/null +++ b/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "net/http" +) + +func main() { + result, err := getIP("https://whatismyipv6.buddy.wtf/json") + if err != nil { + log.Fatal(err) + } + fmt.Println(result.IP) +} + +type whatIsMyIPResult struct { + IP string `json:ip` + Version string `json:version` +} + +func getIP(url string) (*whatIsMyIPResult, error) { + result, err := http.Get(url) + if err != nil { + return nil, err + } + defer result.Body.Close() + ipResult := &whatIsMyIPResult{} + if err := json.NewDecoder(result.Body).Decode(ipResult); err != nil { + return nil, err + } + return ipResult, nil +}