2

I have my php code. How to create something like this in 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);
?>
1

1 Answer 1

4

Something like this:

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)
}
Sign up to request clarification or add additional context in comments.

3 Comments

How can I add additional parametrs to URL from array? For example: https://api.twitter.com/1.1/search/tweets.json?code=253&demo=32535
@leliwa19 what do you mean by parameters from array? Take a look at this -- play.golang.org/p/zFfiMy2C0i. Note that you'll need to do your own escaping of keys and values to keep the URL valid.
Note that you need to close res.Body.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.