匿名类型声明通过“ var”和“:=”的工作方式不同

While declaring a variable with anonymous type in Go, I'm seeing var v versus v:= syntaxes work differently. Imagine we're declaring an empty anonymous struct type instance and assign it to a variable.

This works:

func main() {
    var  v struct {}
    _ = v

But this does not:

func main() {
    t := struct{}
    _ = t
}

compiling this gives the following error (https://play.golang.org/p/MgbttbBVmYE):

prog.go:8:7: type struct {} is not an expression

Why is this the case?

var v struct{} gives v type struct{} but doesn't explicitly set a value, so it gets the zero value {}.

t := struct{} isn't a complete expression. You'd need t := struct{}{} to create t with type struct {} and give it the value {}.

In other words, struct{} is a type, but creating t with := needs a value, not just a type on the right side. struct{}{} is how you write the literal form of an anonymous empty struct.