Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Not and issue] The wording in Receiver Type subject in Wiki is not clear #39917

Closed
bentcoder opened this issue Jun 29, 2020 · 3 comments
Closed

Comments

@bentcoder
Copy link

Hi,

This is not an issue but since I am not sure where to ask this question, I though this would be most ideal place given that the advice is given in this repo. Would be much appreciated if answered/made clearer.

Thanks


https://github.com/golang/go/wiki/CodeReviewComments#receiver-type

Receiver Type
...
If the receiver is a struct that contains a sync.Mutex or similar synchronizing field, the receiver must be a pointer to avoid copying.

If I a have a struct which contains another struct and that one contains an another ..... (you get what I mean, sub field after sub field), should my struct be a pointer? Or, are we talking about my struct's immediate field, not the sub fields and all that?

Some random example.

type Myself struct {
  name string
  address Address
  account *Account
}

type Address struct {
  line string
  city string
  residents []*Resident
}

type Account struct {
  no int
  balance string
  mux sync.Mutex
}

// Like this?
func (m Myself) xyz() {
  // m will never be mutated nor its fields
}

// Like this?
func (m *Myself) xyz() {
  // m will never be mutated nor its fields
}
@ianlancetaylor
Copy link
Contributor

We are talking about immediate fields and also sub-fields, other than sub-fields that exist due to an embedded pointer type.

This is best discussed on a forum rather than the issue tracker. See https://golang.org/wiki/Questions. Thanks.

@bentcoder
Copy link
Author

@ianlancetaylor Before I go away, based on the example above which one is correct please? Asking because "... other than ..." addition got me confused a bit.

func (m Myself) xyz()
func (m *Myself) xyz()

@ianlancetaylor
Copy link
Contributor

I'm sorry, I don't know what you mean by "correct." Either choice could be correct, depending on the goals.

Again, you will get better and faster answers using a forum.

@golang golang locked and limited conversation to collaborators Jul 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants