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

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

BentCoder opened this issue Jun 29, 2020 · 3 comments

Comments

@BentCoder
Copy link

@BentCoder BentCoder commented Jun 29, 2020

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

@ianlancetaylor ianlancetaylor commented Jun 29, 2020

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

@BentCoder BentCoder commented Jul 12, 2020

@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

@ianlancetaylor ianlancetaylor commented Jul 12, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.