Trying to use Go's http package, I can't work out the syntax of .Read
. The following marked by HERE is the only thing I have got to compile, although I tried several other things which were all rejected by the compiler.
package main
import "fmt";
import "http";
import "os";
func main () {
kinopiko_flair := "http://stackoverflow.com/users/flair/181548.json";
response, _, error := http.Get (kinopiko_flair);
if (error != nil) {
// I want to print out the error too.
fmt.Printf ("Error getting %s\n", kinopiko_flair);
os.Exit (1);
}
fmt.Printf ("Status is %s\n", response.Status);
var nr int;
var buf []byte;
nr, error = response.Body.Read (buf); // HERE
if (error != nil) {
// I want to print out the error too.
fmt.Printf ("Error reading response.\n");
os.Exit (1);
}
response.Body.Close ();
fmt.Printf ("Got %d bytes\n", nr);
fmt.Printf ("Got '%s'\n", buf);
}
The URL is OK, since wget
gets it fine, but when I run this buf
is just an empty string and nr
is always zero. What do I need to do to get the data out of response
? The compiler rejected .ReadAll
and other things I tried.
The output looks like this:
Status is 200 OK Got 0 bytes Got ''