Joined February 2013
·

Teddy Sudol

New Jersey
·
·

Posted to Simple TCP Server in Go over 1 year ago

Instead of the double loop to convert the string array to a byte array, why not do:

// import "bytes"
var str = []string{"Hi there!"}
var buf bytes.Buffer
for _, s := range str {
    buf.WriteString(s)
}
_, err = conn.Write(buf.Bytes())

To simplify things further, you could import the "strings" library and call strings.Join() on the str array.

If net.Conn complies with the io.Reader interface, you could also use bytes.Buffer for reading from the connection. I don't think it does comply, however.

Achievements
72 Karma
0 Total ProTip Views