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

Go 2: Make life easier #28760

Closed
pjebs opened this issue Nov 13, 2018 · 4 comments
Closed

Go 2: Make life easier #28760

pjebs opened this issue Nov 13, 2018 · 4 comments

Comments

@pjebs
Copy link
Contributor

pjebs commented Nov 13, 2018

Suppose I have a variable:

Example 1:

var x *int
x = &5 // This fails.

Example 2:

var t *time.Time
t = &time.Now() // this fails

The recommended way is:


var x *int

var dummy int = 5 
x = &dummy // This works

Or

var t *time.Time

var dummy time.Time = time.Now()
t = &dummy // This works

This is annoying. Having to create dummy variables just to do this.

There is also an "apparent" inconsistency (despite following similar semantics to above):

type A struct {}

var x *A

x = &A{} // This is allowed
@pjebs
Copy link
Contributor Author

pjebs commented Nov 13, 2018

My proposal is to provide this syntax sugar:

Example 1:

var x *int
x = &5  equivalent to  x = &[]int{5}[0]

Example 2:

var t *time.Time
t = &time.Now()  equivalent to   t = &[]time.Time{time.Now()}[0]

@pjebs pjebs changed the title Go 2: Make it easier to set pointer variables Go 2: Make life easier Nov 13, 2018
@pjebs
Copy link
Contributor Author

pjebs commented Nov 13, 2018

I see this dummy variable style absolutely everywhere in goland. It is annoying.
The alternative (temporary slice) which I use everywhere is more verbose and looks revolting.

My doctor has given me a magic pill to minimise my migraines due being stuck between a rock and a hard place.

@randall77
Copy link
Contributor

Dup of #9097 (and more directly of #19966, which was closed in favor of #9097). Closing as a dup.

@pjebs
Copy link
Contributor Author

pjebs commented May 25, 2019

I've created https://github.com/rocketlaunchr/igo to demo how it can be used.

@golang golang locked and limited conversation to collaborators May 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants