我正在尝试使一些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)
我很困惑,因为它清楚地实现了接口。如何解决此错误?
2
我至少两次遇到这个问题,而Google搜索解决方案确实没有帮助。
—
凯文·伯克
请注意,不必创建bufio。只需将&b用作io.Writer
—
Vivien,