我想让一些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)
我很困惑,因为它明显实现了接口。我该如何解决这个错误?