On Sat, Jun 23, 2012 at 10:28 AM, Jim Smart <
j...@jimsmart.org> wrote:
> Also, can someone also please tell me what the recommended method for
> emptying / resetting an array to zero length is? I am currently attempting
> to do this using i.bars = make([]Bar, 0) -- but I am now wondering if that
> is correct?
Slices have an length and a capacity.
If you want to length to be zero but want to increase the length again
later you probably want to keep the same capacity so you can avoid
making additional allocations.
i.bars = i.bars[:0]
ie. a slice from the beginning of the slice up to but not including
the 0th element will give you a zero length slice that still has it's
original capacity.
If you want to reset the capacity too because it might have been very
large and you want to free that memory, then you just make a new
slice.
If you want the slice to have zero length and zero capacity then you
can just set it to nil.
eg.
i.bar = nil
or you can specify a length and/or a capacity.
eg.
i.bars = make([]Bar,0,100) //zero length, 100 capacity.
- jessta
--
=====================
http://jessta.id.au