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
Open

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

nightlyone opened this issue Feb 8, 2016 · 6 comments

Comments

@nightlyone
Copy link
Contributor

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
Copy link
Contributor

bradfitz 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
Copy link
Contributor

adg commented May 27, 2016

Probably belongs in the style wiki page.

@nightlyone
Copy link
Contributor Author

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

@nightlyone
Copy link
Contributor Author

ok @adg It started a poll instead https://groups.google.com/forum/?fromgroups&nomobile=true#!topic/golang-nuts/OltVvy8T_ZM to get more feedback.

@davecheney
Copy link
Contributor

@adg
Copy link
Contributor

adg 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
Development

No branches or pull requests

4 participants