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

Golang | Method Sets (values, pointers) #99

Open
atc0005 opened this issue Nov 16, 2020 · 2 comments
Open

Golang | Method Sets (values, pointers) #99

atc0005 opened this issue Nov 16, 2020 · 2 comments

Comments

@atc0005
Copy link
Owner

atc0005 commented Nov 16, 2020

Scratch notes

Rules in the Go spec that define what methods, for a particular type, get bound to the value of that type:

  • value types, require value semantics (value receivers)

    • only those methods implemented with value semantics are attached to values
  • pointer types, allow pointer or value semantics (value or pointer receivers)

    • all methods defined using pointer semantics or value semantics
    • the full method set of behavior is attached to pointers or addresses

Relevant to method sets for implementing interfaces.

Values of type user do not implement the interface because pointer receivers don't belong to the method set of a value.

References

@atc0005
Copy link
Owner Author

atc0005 commented Nov 16, 2020

Still images from the YouTube video:

image

image

image

@atc0005
Copy link
Owner Author

atc0005 commented Oct 26, 2022

Quotes from the YouTube video:

You are never allowed to make a copy of a value that a pointer points to. No integrity in that.

By not including pointer semantic methods the compiler is restricting the ability of this shared piece of data to be copied. We don't want that.

If you are in value semantic mode, we want copies of data to be stored inside of the interface. We want to maintain our value semantic consistency.

If you choose pointer semantics you are restricted to just share. If you choose value semantics let's copy, however there are times where you are in value semantic mode you might need to switch to pointer semantics (e.g., decoding, unmarshalling) and once you switch into pointer semantic mode you are locked in until you come back out of that call chain.

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

No branches or pull requests

1 participant