I am trying to use net.Buffers as follows, to read a string into it:
package main
import (
"fmt"
"net"
)
func main() {
msg := "Hello"
var bufs net.Buffers
n, err := bufs.Read([]byte(msg))
if err != nil {
fmt.Println("bufs.Read error:", err)
return
}
fmt.Printf("Read %d bytes", n)
}
However, when run bufs.Read(...)
fails with:
bufs.Read error: EOF
Contrary to what I thought bufs.Read(...)
is NOT reading the input argument of this API into itself.
So, how can I fill bufs
with some data, that can be written to something like a net.Conn
?
net.Buffers.Read
is a destination slice. You've not written anything to the buffer, and are asking for a 5-byte read -- hence EOF. Even if the read succeeded, the destination slice is unnamed, so couldn't be read.Read
reads a string into the buffer, rather than reads a string out of the buffer.Write
writes a string into the buffer.