http://play.golang.org/p/BgnHN-GikU
var p1 = new(int)
var p2 *int = new(int)
var p3 = 0
var p4 *int
func main() {
fmt.Println(*p1, &p1, p1)
fmt.Println()
fmt.Println(*p2, &p2, p2)
fmt.Println()
fmt.Println(p3, &p3)
fmt.Println()
fmt.Println(p4, &p4)
}
0 0x206a10 0x104382e0
0 0x206a14 0x104382f0
0 0x21ccc0
<nil>
0x206a18
It's not the address size what is different, it's the size (length) of the hexadecimal representation as you printed it.
All addresses you print on the Go Playground are 4 bytes, but if the first bytes (or bits) are zero, they are not printed.
Also if you take a closer look, addresses of your global vars have 6 hexa digits, pointers allocated and returned by new()
have 8 hexa digits. This is because those int
s (returned by new()
) are allocated on the heap with bigger offset (and thus "bigger" memory address).
For example:
var i, j int32 = 123, 123000
fmt.Printf("%x %x
", i, j)
Prints 7b 1e078
even though both numbers are 4 bytes long (32 bits). You may use a format string to add padding 0
s like this:
fmt.Printf("%08x %08x
", i, j)
Which results in 0000007b 0001e078
but this will always pad to 8 digits even if i
or j
would be "less" than 4 bytes (e.g. int16
), so this padding won't tell you if they are of different sizes.