Go

what is the difference between ```var tmp *string``` and ```tmp := new(string)```

what is the difference between
var tmp *string

and
tmp := new(string)
You already invited:

leo

Upvotes from:

the first one creates the pointer (set to `nil`) and the second one creates a string set to its zero value (empty string)
except you're creating a pointer on the first one and a value on the second one - you could have done `tmp := new(*string)` and they would have been functionally identically (both `nil`)
that being said, I never do the `new(variable)` thing

Brian

Upvotes from:

If you print more information
 
// "%v %#v %p
new(string): 0x40c128 (*string)(0x40c128) 0x40c128
*string: nil (*string)(nil) 0x0


`var tmp *string` is just declaration so no memory allotted, whereas in `new(string(` its declared and initialised, so you could print the value too. (edited)

If you wanna answer this question please Login or Register