I've got a Go TCP server which accepts connections, I'd like to echo the messages back 1 byte at a time, I can't see a way to get net.Conn to send a single byte using net.Conn.Write
c.Write([]byte(b)) cannot convert b (type byte) to type []byte
c.Write(b) cannot use b (type byte) as type []byte in argument to c.Write
an io.Writer
always accepts a []byte
as the argument. Use a 1 byte long byte slice. What you tried ([]byte(b)
) was to convert a single byte to a byte slice. Instead, create a one-element byte slice with b
as the only element:
n, err := c.Write([]byte{b})
Use b[i:i+1]
to create a one byte slice:
s := "Hello world!"
b := []byte(s)
for i:=0; i<len(s); i++ {
c.Write(b[i:i+1])
}