nspcc-dev / nspcc-dev/.github

Pointer vs Value receiver in GO

Open
#29 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature go I2 S3 U4
Dominant language
No language data
Stars
2
Forks
1
Avg merge
1h 47m
Merged PRs (30d)
3

Description

Is your feature request related to a problem? Please describe.

I'm always frustrated when I want to start a thread on a review more than once if it ends every time with no conclusion about what we (as a team) think about it. Now I want to fix whether we think using mixed receivers for the same struct is acceptable or not.

Describe the solution you'd like

Write a rule in this repo about our code style with receivers: every struct always has either a pointer or a value receiver for every method.

Describe alternatives you've considered

Do not have any rules about it.

Additional context

Go developers do not recommend using different methods set for values and pointers: https://go.dev/doc/faq#methods_on_values_or_pointers. I also do not like code like thisFunsUsesPointerInterface(&someVar) because it can lead to errors if a dev is not careful enough. In fact, I do not think it is required to use & and * in code at all, not sure why it is important to use pointers in some cases and values in others. If a type is acquired from some external code, it should use it as is, and do not convert it to a pointer/value every time trying to predict whether it is can/should be changed or not.

It is not a problem that I want to fix and change when I see mixed receivers, but I do not understand when there are additional commits that do change a receiver and when there are new threads in PRs that do request to change a receiver.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file or test is named. Start by reviewing the Go methods FAQ linked in the issue and the existing 10-comment discussion, then determine whether the repository should define a receiver rule. Done means the team has reached a decision and the agreed code-style guidance is documented in the appropriate repository location.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.