On 20 October 2017 at 05:15, Feby Tanzil <febytan...@gmail.com> wrote: > Hi, > > I got vague answers in the internet over this. > Which is better & preferable in Go? > > type T struct { > // some attributes > ... > } > > func a() []T { > > } > > func b() []*T { > > }
Both are plausible. It depends what you're going to do with the slice and its elements. Are the methods on T pointer or value methods (if they're pointer methods, it's probably best to use []*T) ? Are you going to append lots of elements to the array (pointers might be faster because there's less to copy)? Are you going to take the address of elements in the array (problematic if you do this and then append to the array, because the addresses may change; also keeping an address of one element will pin the entire array)? Are you doing lots of computation on values directly inside T ([]T might give you a performance boost via cache locality)? I usually give most weight to the first criterion above - store objects in the form you're most likely to use for passing them around. -- 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.