如何转换(输入* bytes.Buffer)作为w.Write参数中的[]字节 [英] How to convert (type *bytes.Buffer) to use as []byte in argument to w.Write

查看:192
本文介绍了如何转换(输入* bytes.Buffer)作为w.Write参数中的[]字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从服务器返回一些json,但使用以下代码获取此错误:

 无法使用缓冲区(类型* bytes.Buffer)作为参数类型[]字节w.Write 

谷歌搜索,我发现这个SO答案,但(请参阅第二个带错误信息的代码示例)



第一个代码示例



<$ p $对于_,jsonRawMessage:=范围sliceOfJsonRawMessages {
,如果err:= json.Compact(buffer,jsonRawMessage),则为缓冲区:= new(bytes.Buffer)

。 ; err!= nil {
fmt.Println(error)

}

}
fmt.Println(json returned,buffer) //这是json
w.Header()。Set(Content-Type,contentTypeJSON)

w.Write(buffer)// error:can not use buffer(type * bytes .Buffer as type [] byte in w.Write

第二个错误代码示例 p>

 不能使用foo(type * bufio.Writer)作为类型* bytes.Buffer参数给json.Compact 
不能使用foo(type * bufio.Writer)as type [] byte in w.Write


var b bytes.Buffer
foo:= bufio.NewWriter(& b )

for _,d:=范围tJ {
if err:= json.Compact(foo,d); err!= nil {
fmt.Println(error)

}

}


w.Header ().Set(Content-Type,contentTypeJSON)

w.Write(foo)


<写需要一个 [] byte (字节片段),并且你有一个 * bytes.Buffer (指向缓冲区的指针)。

您可以使用 Buffer.Bytes()并将其赋予 Write()

  _,err = w.Write(buffer.Bytes())

...或使用 Buffer.WriteTo()将缓冲区内容直接复制到 Writer

  _,err = buffer.WriteTo(w)

使用 bytes.Buffer 不是绝对必要的。 json.Marshal()返回一个 []字节直接:

  var buf [] byte 

buf,err = json.Marshal(thing)

_,err = w.Write(buf)


I'm trying to return some json back from the server but get this error with the following code

cannot use buffer (type *bytes.Buffer) as type []byte in argument to w.Write

With a little googling, I found this SO answer but couldn't get it to work (see second code sample with error message)

1st code sample

buffer := new(bytes.Buffer)

for _, jsonRawMessage := range sliceOfJsonRawMessages{
    if err := json.Compact(buffer, jsonRawMessage); err != nil{
        fmt.Println("error")

    }

}   
fmt.Println("json returned", buffer)//this is json
w.Header().Set("Content-Type", contentTypeJSON)

w.Write(buffer)//error: cannot use buffer (type *bytes.Buffer) as type []byte in argument to w.Write

2nd code sample with error

cannot use foo (type *bufio.Writer) as type *bytes.Buffer in argument to json.Compact
 cannot use foo (type *bufio.Writer) as type []byte in argument to w.Write


var b bytes.Buffer
foo := bufio.NewWriter(&b)

for _, d := range t.J{
    if err := json.Compact(foo, d); err != nil{
        fmt.Println("error")

    }

}


w.Header().Set("Content-Type", contentTypeJSON)

w.Write(foo)

解决方案

Write requires a []byte (slice of bytes), and you have a *bytes.Buffer (pointer to a buffer).

You could get the data from the buffer with Buffer.Bytes() and give that to Write():

_, err = w.Write(buffer.Bytes())

...or use Buffer.WriteTo() to copy the buffer contents directly to a Writer:

_, err = buffer.WriteTo(w)

Using a bytes.Buffer is not strictly necessary. json.Marshal() returns a []byte directly:

var buf []byte

buf, err = json.Marshal(thing)

_, err = w.Write(buf)

这篇关于如何转换(输入* bytes.Buffer)作为w.Write参数中的[]字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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