Hacker News new | ask | show | jobs
by zenocon 4045 days 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.

1 comments

What 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.