On Tuesday, 31 January 2017 00:15:40 UTC-5, Keiji Yoshida wrote:
>
> Hi,
>
> "Declaring Empty Slices" of CodeReviewComments ( 
> https://github.com/golang/go/wiki/CodeReviewComments#declaring-empty-slices 
> ) says as below:
>
> ```
> When declaring a slice, use
>
> var t []string
>
> rather than
>
> t := []string{}
>
> The former avoids allocating memory if the slice is never appended to.
> ```
>
> I executed a benchmark test against above code but I could not see any 
> difference between them as for the results.
>
> My benchmark test code and its results can be seen here: 
> https://github.com/keijiyoshida/go-code-snippets/blob/master/empty-slice-declaration/main_test.go
>
> Is there something wrong in my benchmark test code or procedure?
>
> Thanks,
> Keiji Yoshida
>

The second form allocates an array of length zero.  Allocating zero-sized 
objects is almost free, since they all live at the same address, regardless 
of type.  (This is an implementation detail, not a necessary consequence of 
the spec.)  The only real difference between the two forms is that the 
first slice is nil whereas the second is empty but non-nil.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to