Last active
November 1, 2015 23:52
-
-
Save ikuwow/9f0749fe3653917d2319 to your computer and use it in GitHub Desktop.
Go言語で簡単にHTTPリクエストを送ってJSONをパースするサンプル ref: http://qiita.com/ikuwow/items/c8f494bbd16adf6db142
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 ( | |
"os" | |
"fmt" | |
"net/http" | |
"github.com/m0a/easyjson" | |
) | |
var api = "https://teratail.com/api/v1" | |
func run() error { | |
resp, err := http.Get(api+"/questions") | |
if err != nil { | |
return fmt.Errorf("Failed to connect teratail.com") | |
} | |
defer resp.Body.Close() | |
jsonData, err := easyjson.NewEasyJson(resp.Body) | |
if err != nil { | |
return fmt.Errorf("Invalid responses") | |
} | |
for _, v:=range jsonData.K("questions").RangeObjects() { | |
fmt.Printf("%s\n", v.K("title")) | |
} | |
return nil | |
} | |
func main() { | |
if err := run(); err != nil { | |
fmt.Fprintf(os.Stderr, "%s\n", err) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment