我有我的PHP代码。如何在Go中创建这样的东西?
<?php
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$context = stream_context_create(array(
'http' => array(
'ignore_errors'=>true,
'method'=>'GET'
)
));
$response = json_decode(file_get_contents($url, false, $context));
print_r($response);
?>
答案 0 :(得分:4)
这样的事情:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func main() {
resp, err := http.Get("https://api.twitter.com/1.1/search/tweets.json")
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Printf("%#v\n", resp)
dec := json.NewDecoder(resp.Body)
if dec == nil {
panic("Failed to start decoding JSON data")
}
json_map := make(map[string]interface{})
err = dec.Decode(&json_map)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", json_map)
}