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

Short variable declaration not working with array dereferencing #31268

Closed
safety-tom opened this issue Apr 5, 2019 · 2 comments
Closed

Short variable declaration not working with array dereferencing #31268

safety-tom opened this issue Apr 5, 2019 · 2 comments

Comments

@safety-tom
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.12.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

N/A

What did you do?

package main

import ("errors";"fmt")

func main() {
	var s [1]int
	s[0], err1 := 1, errors.New("some error")  // error: non-name s[0] on left side of :=
	fmt.Printf("%v\n", err1)
	
	var err2 error
	s[0], err2 = 1, errors.New("some error") // all good	
	fmt.Printf("%v %v\n", s[0], err2)
	
	var i int
	i, err3 := 1, errors.New("some other error") // all good	
	fmt.Printf("%v %v", i, err3)
}

You can try it out here: https://play.golang.org/p/OIy7TJWnSkF

What did you see?

  • if I change the operation to multiple assignment (pre-initialise err2), everything works as expected.
  • if instead of s[i] (dereferencing an array) I use a single value (var i int), the mixed assignment/short declaration works as advertised.

What did you expect to see?

  • at best I would expect Go to compile the program (not an unreasonable expectation IMO; given that multiple initialisation works, I don't see why short var declaration wouldn't).
  • at the very least I would expect a better compiler error.
@go101
Copy link

go101 commented Apr 5, 2019

@mvdan
Copy link
Member

mvdan commented Apr 5, 2019

Like @go101 said, see the previous issues and proposals.

@mvdan mvdan closed this as completed Apr 5, 2019
@golang golang locked and limited conversation to collaborators Apr 4, 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

4 participants