Last active
April 3, 2016 21:13
-
-
Save nickvanw/496db8a186bc969318c8d8109e5e969e to your computer and use it in GitHub Desktop.
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 ( | |
"encoding/json" | |
"fmt" | |
"net" | |
"net/http" | |
"sync" | |
) | |
type IPResponse struct { | |
IP net.IP `json:"ip"` | |
} | |
func IPRequest(site string) (*IPResponse, error) { | |
req, err := http.NewRequest("GET", site, nil) | |
if err != nil { | |
return nil, err | |
} | |
req.Header.Add("Accept", "application/json") | |
req.Header.Add("User-Agent", "HTTPie/1.0.0-dev") | |
resp, err := http.DefaultClient.Do(req) | |
if err != nil { | |
return nil, err | |
} | |
defer resp.Body.Close() | |
var ip IPResponse | |
err = json.NewDecoder(resp.Body).Decode(&ip) | |
return &ip, err | |
} | |
func main() { | |
var wg sync.WaitGroup | |
wg.Add(2) | |
go func() { | |
fmt.Println(IPRequest("http://jsonip.com")) | |
wg.Done() | |
}() | |
go func() { | |
fmt.Println(IPRequest("http://ipinfo.io")) | |
wg.Done() | |
}() | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment