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

cmd/compile: "var _ = composite literal" causes a compile error inside a function #23528

guangda-hu opened this issue Jan 24, 2018 · 1 comment


Copy link

@guangda-hu guangda-hu commented Jan 24, 2018

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

Go playground

What did you do?

package main

type T struct{ int }

type M map[int]int

type A [1]int

func main() {
	var _ = map[int]int{0: 0} // error
	var _ = M{0: 0} // error

	var _ = [1]int{0} // error
	var _ = A{0} // error

	var _ = struct{ int }{0} // error
	var _ = T{0} // error

There will be no problems if we apply any one of the following changes:

  1. Move the declarations out of main();
  2. Change them to assignments, i.e. remove "var";
  3. Remove the contents inside {}, e.g. var _ = map[int]int{} can be compiled without any problem;
  4. Add a type after the blank identifier:
    var _ M = map[int]int{0: 0}
    var _ map[int]int = M{0: 0}
    both work. (But var _ map[int]int = map[int]int{0: 0} won't work.)


What did you expect to see?

The spec mentions

the variables are initialized with the expressions following the rules for assignments

in the variable declaration section, and

The blank identifier provides a way to ignore right-hand side values in an assignment

in the assignment section.
From this point of view, the variable declarations should probably work, though I'd say the spec is actually not clear on this.
Since the declarations work outside the function, and assignments (i.e. without "var") work inside the function, I understand that these kind of declarations are useless. This is just some weird interesting behaviors.


This comment has been minimized.

Copy link

@dominikh dominikh commented Jan 24, 2018

Closing as duplicate of #15481

@dominikh dominikh closed this Jan 24, 2018
@golang golang locked and limited conversation to collaborators Jan 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

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