I built my first Go Lang program to request some data from the Topsy Otter API that queries Twitter.
It returns JSON and requests via HTTP
It doesn't unmarshall the JSON just yet...
package main
import (
//"io"
"io/ioutil"
//"strings"
//"log"
"http"
"json"
"fmt"
)
/*
{
"request" : {
"parameters" : {
"url" : "http://twitter.com/barackobama"
},
"response_type" : "json",
"resource" : "authorinfo",
"url" : "http://otter.topsy.com/authorinfo.json?url=http%3A%2F%2Ftwitter.com%2Fbarackobama"
},
"response" : {
"name" : "Barack Obama",
"url" : "http://twitter.com/barackobama",
"type" : "twitter",
"nick" : "barackobama",
"description" : "44th President of the United States",
"topsy_author_url" : "http://topsy.com/twitter/barackobama",
"influence_level" : "5"
}
}
*/
type OtterResponse struct {
name string
Url string
Type string
Nick string
Description string
Topsy_author_url string
Influence_level string
}
func main() {
r, err := http.Get("http://otter.topsy.com/authorinfo.json?url=http://twitter.com/barackobama")
var b []byte;
if err == nil {
b, err = ioutil.ReadAll(r.Body);
r.Body.Close();
}
if err != nil {
fmt.Println(err)
fmt.Println("Whoa we really didn't make it!")
} else {
fmt.Println(string(b));
var m OtterResponse
err := json.Unmarshal(b, &m)
if err != nil {
fmt.Println("Whoa we didn't make it!")
fmt.Println(err)
} else {
fmt.Println("Whoa we made it!")
fmt.Println(m.name)
}
}
}
The links I used:
http://blog.golang.org/2011/01/json-and-go.html
http://code.google.com/p/otterapi/wiki/Resources
http://golang.org/pkg/http/#Response
http://raycompstuff.blogspot.com/2009/11/golang-networking.html
All of the interesting technological, artistic or just plain fun subjects I'd investigate if I had an infinite number of lifetimes. In other words, a dumping ground...
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment