Should the phrasing about method sets in Go FAQ be slightly clarified?

103 views
Skip to first unread message

Timur Sharapov

unread,
Jul 1, 2024, 11:13:05 AM (7 days ago) Jul 1
to golang-nuts
https://go.dev/doc/faq#different_method_sets

I am referring to this paragraph that in my opinion answers one of the trickiest questions for new Go developers. 

Even in cases where the compiler could take the address of a value to pass to the method, if the method modifies the value the changes will be lost in the caller. As an example, if the Write method of bytes.Buffer used a value receiver rather than a pointer, this code:

var buf bytes.Buffer
io.Copy(buf, os.Stdin)
would copy standard input into a copy of buf, not into buf itself. This is almost never the desired behavior.


Even though I understand the explanation, I think that it's a bit unclear. 
If the Write method used a value receiver, then the whole example doesn't make sense and the part "Even in cases where the compiler could take the address of a value" doesn't apply at all because the code works as is, and the compiler doesn't really have to take any addresses. 

My bet is that the document meant "if the code above worked..."

What do you think? 

Ian Lance Taylor

unread,
Jul 2, 2024, 1:55:36 PM (5 days ago) Jul 2
to Timur Sharapov, golang-nuts
I agree that this isn't quite right. It may read better if we put a
paragraph break between the "even in cases" sentence and the rest.

Ian
Reply all
Reply to author
Forward
0 new messages