3 ms·
This is cool, but I find this snippet a tad bizarre: client, server := net.Pipe() var buf bytes.Buffer client = &recordingConn {
by zenocon 11y ago
This is cool, but I find this snippet a tad bizarre:
client, server := net.Pipe()
var buf bytes.Buffer
client = &recordingConn {
Conn: client,
Writer: io.MultiWriter(client, &buf),
}
While it seemingly works, it seems confusing to assign client to an internal field of a new struct, and then overwriting that same client variable with the newly created struct...not to mention passing it into MultiWriter.
Anyway, I like those short posts like this. I've done similar things with embedding and struct "reconstruction" in order to elicit more testable code. It's powerful and useful, indeed.
- gohrt 11y agoWhat is the benefit of re-using the 'client' identifier? There's no closure capturing the identifier. Compare: client, server := net.Pipe() var buf bytes.Buffer client = &recordingConn { Conn: client, Writer: io.MultiWriter(client, &buf), } vs tempClient, server := net.Pipe() var buf bytes.Buffer client = &recordingConn { Conn: tempClient, Writer: io.MultiWriter(tempClient, &buf), } The latter is less confusing to me.
- deleted 11y ago[deleted]
- deleted 11y ago[deleted]