Skip to content

Instantly share code, notes, and snippets.

@nickvanw
Created April 3, 2016 21:19
Show Gist options
  • Save nickvanw/87baa2a97522732b18400470644e62a0 to your computer and use it in GitHub Desktop.
Save nickvanw/87baa2a97522732b18400470644e62a0 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
)
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 mustReturn(site string, out chan *IPResponse) {
o, _ := IPRequest(site)
out <- o
}
func main() {
out := make(chan *IPResponse)
go mustReturn("http://jsonip.com", out)
go mustReturn("http://ipinfo.io/", out)
for i := 0; i < 2; i++ {
ret := <-out
fmt.Printf("Got: %s\n", ret.IP)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment