Skip to content

Commit

Permalink
_content/doc/faq: clarify the explanation of method sets
Browse files Browse the repository at this point in the history
I had a small discussion with @ianlancetaylor here:
https://groups.google.com/g/golang-nuts/c/uon32LDerjM/m/dvg0mxbwAAAJ?utm_medium=email&utm_source=footer

I would like to clarify this entry about method sets a bit because the current explanation is a bit confusing. I believe there is no reason for the Write method of Buffer to use a value receiver. In this case, the sentence about the compiler's ability to implicitly take addresses doesn't make sense.

What I believe is implied here is a hypothetical situation where the code from the example is allowed by Go, and then it is noted that this makes no sense as the result would be lost in the caller.

Change-Id: Ie4de77364165d59178e2b1e6e1393370e06d1755
GitHub-Last-Rev: 40392d4
GitHub-Pull-Request: #296
Reviewed-on: https://go-review.googlesource.com/c/website/+/601996
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
  • Loading branch information
tsharapov authored and gopherbot committed Jul 31, 2024
1 parent d2408a1 commit 04a44d2
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions _content/doc/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -1425,19 +1425,17 @@ the language specification.)
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`](/pkg/bytes/#Buffer)
used a value receiver rather than a pointer,
this code:

As an example, if the code below were valid:

```
var buf bytes.Buffer
io.Copy(buf, os.Stdin)
```

would copy standard input into a *copy* of `buf`,
it would copy standard input into a *copy* of `buf`,
not into `buf` itself.
This is almost never the desired behavior.
This is almost never the desired behavior and is therefore disallowed by the language.

### What happens with closures running as goroutines? {#closures_and_goroutines}

Expand Down

0 comments on commit 04a44d2

Please sign in to comment.