Created
January 7, 2017 05:43
-
-
Save gnilchee/e82df5097bb29e908cfcb29b851fbcd4 to your computer and use it in GitHub Desktop.
Using json and net/http get, parse and select data from web endpoint response.
This file contains hidden or 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" | |
"log" | |
"net/http" | |
"io/ioutil" | |
"encoding/json" | |
) | |
type mytype map[string]string | |
func main() { | |
res, err := http.Get("https://api.github.com/") | |
if err != nil { | |
log.Fatal(err) | |
} | |
github, err := ioutil.ReadAll(res.Body) | |
res.Body.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
var data mytype | |
err = json.Unmarshal(github, &data) | |
if err != nil { | |
log.Fatal(err) | |
} | |
current_user := data["current_user_url"] | |
events_url := data["events_url"] | |
rate_limit := data["rate_limit_url"] | |
fmt.Println("Current User:", current_user) | |
fmt.Println("Events URL:", events_url) | |
fmt.Println("Rate Limit URL:", rate_limit) | |
for k, _ := range data { | |
fmt.Printf("%s\n", k) | |
} | |
for _, v := range data { | |
fmt.Printf("%s\n", v) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment