debuggoCritical
Getting "bytes.Buffer does not implement io.Writer" error message
Viewed 0 times
byteserrorgettingwriterdoesmessagenotbufferimplement
Problem
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
I get the following error:
I am confused, since it clearly implements the interface. How do I resolve this error?
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?
Solution
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)
}Code Snippets
import "bufio"
import "bytes"
func main() {
var b bytes.Buffer
foo := bufio.NewWriter(&b)
}Context
Stack Overflow Q#23454940, score: 191
Revisions (0)
No revisions yet.