2

我有我的 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);
?>
4

1 回答 1

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)
}
于 2013-07-18T12:05:35.593 回答