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

aaronbuchwald opened this issue Jun 30, 2020 · 1 comment


Copy link

@aaronbuchwald aaronbuchwald commented Jun 30, 2020

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

$ go version

Does this issue reproduce with the latest release?


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 {
	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.

Copy link

@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 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.