Declaring a variable vs initializing it with zero value
In Go, all variables declared without initializers are initialized to the so-called zero values appropriate for their types. Since for a struct person
its zero value is the expression person{}
both cases, (1) and (2) do the same thing. I reckon the compiler will generate the same code for them.
To cite the spec:
A variable declaration creates a variable, binds an identifier to it and gives it a type and optionally an initial value.
…
If a list of expressions is given, the variables are initialized by assigning the expressions to the variables in order; all expressions must be consumed and all variables initialized from them. Otherwise, each variable is initialized to its zero value.
Memory allocation by declaring a variable
The declaration var i int
declares a variable and initializes it. In some sense, this does mean allocating memory (and initializing it) but I'd not dig that deep: you should be fine with the fact the compiler will arrange for the variable to exist.
As to whether that "denotes an address" or not depends on how you look at this: it's not the programmer's business to think about how the variable is laid out in memory—the name of the variable is a handle on its value, and the compiler is free to provide such access however it pleases. I mean, the compiler may internally store the address of this variable somewhere and use it but you should not be concerned with it.
The var i *int = new(int)
declaration declares a pointer to an anonymous variable (a memory block which has the semantics of a certain type "attached" to it), initializes the memory of that variable with the zero value appropriate for its type and then assigns the address of that memory block to the variable you're declaring.
To cite the spec:
The built-in function new
takes a type T
and returns a value of type *T
. The memory is initialized as described in the section on initial values.
In this case, you operate on a pointer, not on a variable itself. So these two declarations do quite different things as they declare different types of variables, and the resulting variables have different semantics.
Possibly the chief difference between var i int
and var p *int = new(int)
lies in that in the former case the variable is created "right away" (statically) while in the latter case the memory for the anonymous variable is initialized dynamically. But again do not be too attached to this: when the execution hits the var i int
declaration, you cannot know for sure what exactly happens in the program other than the variable named i
of type int
becomes available. For instance, the compiler is free to use "the heap" to allocate it, not "the stack" (assuming a typical H/W architecture).