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

doc: add guidance on new vs. literal usage #14255

Open
nightlyone opened this issue Feb 8, 2016 · 6 comments

Comments

@nightlyone
Copy link
Contributor

commented Feb 8, 2016

One thing I observed with new gophers, is having trouble judging is when to use new and when to take the address of struct literal.

Adding a bit of guidance in the wiki or effective_go.html would be quite useful.

Suggestion for guidance:

  • prefer the literal
  • if you don't initialize the struct with values, use new
  • if you did use new, don't initialize values later, prefer switching the code to a literal instead.

@bradfitz bradfitz added this to the Unreleased milestone Feb 8, 2016

@bradfitz

This comment has been minimized.

Copy link
Member

commented Feb 8, 2016

Or something at https://golang.org/s/style

@ianlancetaylor ianlancetaylor changed the title add guidance on new vs. literal usage doc: add guidance on new vs. literal usage Feb 8, 2016

@adg adg removed their assignment May 27, 2016

@adg

This comment has been minimized.

Copy link
Contributor

commented May 27, 2016

Probably belongs in the style wiki page.

@nightlyone

This comment has been minimized.

Copy link
Contributor Author

commented Aug 20, 2016

@adg I could add this to the style wiki, if you are ok with the suggestions itself. Then we can close this issue.

@nightlyone

This comment has been minimized.

Copy link
Contributor Author

commented Aug 20, 2016

@davecheney

This comment has been minimized.

@adg

This comment has been minimized.

Copy link
Contributor

commented Aug 21, 2016

@davecheney that blog post doesn't mention new at all. Did you mean to refer to another post?

I would not advise someone to switch from new(T) to &T{} or vice versa. The main piece of advice I agree with is your third point, which is not to use new followed by initializing fields with assignments. Not sure it really needs to be spelled out, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.