获取“bytes.Buffer does not implement io.Writer”错误消息

huangapple go评论78阅读模式
英文:

Getting "bytes.Buffer does not implement io.Writer" error message

问题

我正在尝试让一个Go对象实现io.Writer接口,但是将数据写入字符串而不是文件或类似文件的对象。我以为bytes.Buffer会起作用,因为它实现了Write(p []byte)方法。然而,当我尝试以下代码时:

import "bufio"
import "bytes"

func main() {
    var b bytes.Buffer
    foo := bufio.NewWriter(b)
}

我得到了以下错误:

cannot use b (type bytes.Buffer) as type io.Writer in function argument:
    bytes.Buffer does not implement io.Writer (Write method has pointer receiver)

我感到困惑,因为它明显实现了该接口。我该如何解决这个错误?

英文:

I'm trying to have some Go object implement io.Writer, but writes to a string instead of a file or file-like object. I thought bytes.Buffer would work since it implements Write(p []byte). However when I try this:

import "bufio"
import "bytes"

func main() {
    var b bytes.Buffer
    foo := bufio.NewWriter(b)
}

I get the following error:

cannot use b (type bytes.Buffer) as type io.Writer in function argument:
    bytes.Buffer does not implement io.Writer (Write method has pointer receiver)

I am confused, since it clearly implements the interface. How do I resolve this error?

答案1

得分: 178

将指向缓冲区的指针传递给函数,而不是传递缓冲区本身:

import "bufio"
import "bytes"

func main() {
    var b bytes.Buffer
    foo := bufio.NewWriter(&b)
}
英文:

Pass a pointer to the buffer, instead of the buffer itself:

import "bufio"
import "bytes"

func main() {
    var b bytes.Buffer
    foo := bufio.NewWriter(&b)
}

答案2

得分: 41

你不需要使用 "bufio.NewWriter(&b)" 来创建一个 io.Writer。&b 本身就是一个 io.Writer。

英文:
package main

import "bytes"
import "io"

func main() {
    var b bytes.Buffer
    _ = io.Writer(&b)
}

You don't need use "bufio.NewWriter(&b)" to create an io.Writer. &b is an io.Writer itself.

答案3

得分: 14

只需使用foo := bufio.NewWriter(&b),因为bytes.Buffer实现了io.Writer接口的方式是:

func (b *Buffer) Write(p []byte) (n int, err error) {
    ...
}

type Writer interface {
    Write(p []byte) (n int, err error)
}

它是b *Buffer,而不是b Buffer。(我也认为通过变量或其指针调用方法很奇怪,但我们不能将指针分配给非指针类型的变量。)

此外,编译器的提示不够清晰:

bytes.Buffer does not implement io.Writer (Write method has pointer receiver)


一些想法,Go使用的是值传递(Passed by value)方式,如果我们将b传递给buffio.NewWriter(),在NewWriter()中,它是一个新的b(一个新的缓冲区),而不是我们定义的原始缓冲区,因此我们需要传递地址&b


bytes.Buffer的定义如下:

type Buffer struct {
    buf       []byte   // contents are the bytes buf[off : len(buf)]
    off       int      // read at &buf[off], write at &buf[len(buf)]
    bootstrap [64]byte // memory to hold first slice; helps small buffers avoid allocation.
    lastRead  readOp   // last read operation, so that Unread* can work correctly.
}

使用值传递方式,传递的新缓冲区结构与原始缓冲区变量不同。

英文:

Just use

foo := bufio.NewWriter(&b)

Because the way bytes.Buffer implements io.Writer is

func (b *Buffer) Write(p []byte) (n int, err error) {
    ...
}
// io.Writer definition
type Writer interface {
    Write(p []byte) (n int, err error)
}

It's b *Buffer, not b Buffer. (I also think it is weird for we can call a method by a variable or its pointer, but we can't assign a pointer to a non-pointer type variable.)

Besides, the compiler prompt is not clear enough:

bytes.Buffer does not implement io.Writer (Write method has pointer receiver)


Some ideas, Go use Passed by value, if we pass b to buffio.NewWriter(), in NewWriter(), it is a new b (a new buffer), not the original buffer we defined, therefore we need pass the address &b.


bytes.Buffer is defined as:

type Buffer struct {
	buf       []byte   // contents are the bytes buf[off : len(buf)]
    off       int      // read at &buf[off], write at &buf[len(buf)]
	bootstrap [64]byte // memory to hold first slice; helps small buffers avoid allocation.
    lastRead  readOp   // last read operation, so that Unread* can work correctly.
}

using passed by value, the passed new buffer struct is different from the origin buffer variable.

huangapple
  • 本文由 发表于 2014年5月4日 18:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/23454940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定