temp{val:5}
is a composite literal, it creates a value of type temp
.
In the first example you used a short variable declaration, which is equivalent to
var variable1 = temp{val: 5}
There is a single variable created here (variable1
) which is initialized with the value temp{val: 5}
.
In the second example you take the address of a composite literal. That does create a variable, initialized with the literal's value, and the address of this variable will be the result of the expression. This pointer value will be assigned to the variable variable2
.
Spec: Compositle literals:
Taking the address of a composite literal generates a pointer to a unique variable initialized with the literal's value.