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 redeclares instead of assigns for multi-variable declaration #39934

Closed
aaronbuchwald opened this issue Jun 30, 2020 · 1 comment

Comments

@aaronbuchwald
Copy link

@aaronbuchwald aaronbuchwald commented Jun 30, 2020

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

$ go version
1.13.8

Does this issue reproduce with the latest release?

Yes

Short variable declaration redeclares variable instead of using pre-existing variable

func main() {
	a, b := 5, 6
	if b == 6 {
		a, c := 7, 8
		if c == 5 {
			return
		}
	}
	fmt.Printf("%d", a)
}

variables a and b are declared using short variable declaration in the first statement. Then, after using short variable declaration to declare a, c within the body of the if statement, it reports that the variable a here is declared but not used.

Expected behavior is that the short variable declaration within the if body, should declare the new variable c and reference the already existing variable a.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jun 30, 2020

Using := only redeclares variables declared in the same block. It does not redeclare variables declared in an enclosing block.

This is documented at https://golang.org/ref/spec#Short_variable_declarations .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.