如何在Go中使用.Read函数? [英] How do I use the .Read function in Go?

查看:171
本文介绍了如何在Go中使用.Read函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Go的http包,我无法计算 .Read 的语法。下面用HERE标记是我编译的唯一东西,尽管我尝试了其他一些被编译器拒绝的东西。

  package main 
importfmt;
导入http;
导入os;

func main(){
kinopiko_flair:=http://stackoverflow.com/users/flair/181548.json;
response,_,error:= http.Get(kinopiko_flair);
if(error!= nil){
//我也想打印出错误。
fmt.Printf(获取%s \ n错误,kinopiko_flair);
os.Exit(1);
}
fmt.Printf(Status is%s\\\
,response.Status);
var nr int;
var buf [] byte;
nr,error = response.Body.Read(buf); // HERE
if(error!= nil){
//我也想打印出错误。
fmt.Printf(Error reading response.\\\
);
os.Exit(1);
}
response.Body.Close();
fmt.Printf(Got%d bytes \\\
,nr);
fmt.Printf(Got'%s'\\\
,buf);
}

URL是可以的,因为 wget 得到它很好,但是当我运行这个 buf 时只是一个空字符串,而且 nr 总是为零。我需要做些什么才能从 response 中获取数据?编译器拒绝了 .ReadAll 和其他我试过的东西。



输出如下所示:

 
状态为200 OK
得到0字节
得到''


解决方案

尝试给切片buf一个大小,例如

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

读取器读取缓冲区的len()。



来自io.go

  // Reader是包装基本Read方法的接口。 
//
//读取最多len(p)个字节到p。它返回字节数
// read(0 <= n <= len(p))和遇到的任何错误。
//即使读取返回n < len(p),
//在调用过程中,它可以将所有p用作临时空间。
//如果有些数据可用但不是len(p)字节,则通常读
//返回可用的数据,而不是等待更多的数据。
//
//在输入流结束时,Read返回0,os.EOF。
//读取可能会返回一个非零数字的字节,并且不为零。
//特别是,耗尽输入的Read可能会返回n> 0,os.EOF。


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 ''

解决方案

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.

这篇关于如何在Go中使用.Read函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆