When using Hijack() with a http.ResponseWriter instance
Hijack() (net.Conn, *bufio.ReadWriter, error)
What is the difference between reading from the net.Conn and the *bufio.ReadWriter?
On
Their difference is buffered IO.
net.Conn implements Read() and Write() thus implementing io.Reader and io.Writer. This is why bufio can wrap it up as a buffered ReadWriter and further implement functions on top using those two methods in a buffered manner.
If you only want to use Read() and Write(), you can just stick with net.Conn, otherwise you can take advantage for the buffered ReadWriter methods.
See https://golang.org/pkg/io/#Reader and https://golang.org/pkg/io/#Writer
net.Conn.Readand*bufio.ReadWriter.Readboth read from the same connection, but the latter is buffered. TheHijackmethod in the standard "net/http" package directly returns thenet.Connwrapped in abufio.ReadWriter, using the same*bufio.Readerthat was already allocated for the http request.It's possible that there is still data buffered in the
bufio.Readerwhich you may miss when reading directly from the network connection. If you want to use thenet.Conndirectly, you should check if there is buffered data already withReader.Buffered, and handle that according to the protocol being used.In general, you should prefer to use
bufio.ReadWriter, as it will be more efficient for non-optimally sized reads and writes to the network.The
net.Connis still needed to handle the Read and Write deadlines, to close thenet.Connwhen you're done, and for any other network-specific activities.