tags:

views:

170

answers:

2

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 ''
+3  A: 

Try giving the slice buf a size, e.g.

 buf := make([]byte,128);

Reader reads up to len() of the buffer it is given.

From io.go

// Reader is the interface that wraps the basic Read method.
//
// Read reads up to len(p) bytes into p.  It returns the number of bytes
// read (0 <= n <= len(p)) and any error encountered.
// Even if Read returns n < len(p),
// it may use all of p as scratch space during the call.
// If some data is available but not len(p) bytes, Read conventionally
// returns what is available rather than block waiting for more.
//
// At the end of the input stream, Read returns 0, os.EOF.
// Read may return a non-zero number of bytes with a non-nil err.
// In particular, a Read that exhausts the input may return n > 0, os.EOF.
Scott Wales
+1  A: 

Trying to adapt the cat example from the tutorial:

fmt.Printf ("Status is %s\n", response.Status);
var nr int;

const NBUF = 512;
var buf [NBUF]byte;

for {
    switch nr, _ := response.Body.Read(&buf); true {
    case nr < 0:
        os.Exit(1);
    case nr == 0:  // EOF
        return;
    case nr > 0:
        if nw, _ := os.Stdout.Write(buf[0:nr]); nw != nr {
            fmt.Printf("error\n");
        }
    }
}

Output:

$ ./8.out 
Status is 200 OK
{"id":181548,"gravatarHtml":"\u003cimg src=\"http://www.gravatar.com/avatar/f4a286fa31d1359ee92113823a70a738?s=50&amp;amp;d=identicon&amp;amp;r=PG\" height=\"50\" width=\"50\" alt=\"\"\u003e","profileUrl":"http://stackoverflow.com/users/181548/kinopiko","displayName":"Kinopiko","reputation":"4,674","badgeHtml":"\u003cspan title=\"1 gold badge\"\u003e\u003cspan class=\"badge1\"\u003e&#9679;\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e1\u003c/span\u003e\u003c/span\u003e\u003cspan title=\"5 silver badges\"\u003e\u003cspan class=\"badge2\"\u003e&#9679;\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e5\u003c/span\u003e\u003c/span\u003e\u003cspan title=\"21 bronze badges\"\u003e\u003cspan class=\"badge3\"\u003e&#9679;\u003c/span\u003e\u003cspan class=\"badgecount\"\u003e21\u003c/span\u003e\u003c/span\u003e"}
ema