[When you are replying to someone else's message, reply to that message,
not your original message.  Your reply lost the context of the message
to which you were replying (Jan Mercl's).]

* Stuart Davies <sdd.dav...@gmail.com> [191015 07:24]:
> My goal is to contain a list of integers that may extend to possibly a 
> couple of thousand entries. Initial size is 100. Extensions add 50 entries 
> at a time.
> 
> I declared the array initially with:
> 
> make([]int64, 100).
> 
> I tried using the Append function but could not Append past the 100 
> entries. The 101 entry threw a panic so I detirmined to extend the array 
> myself. 
> 
> I would have thought Append should extend if required but it did not (or am 
> I using it incorrectly). I looked at some other code examples but could not 
> find a solution.
> 
> In C there is an array copy function that is implemented by the processor 
> directly, it's been a long time since I used C and assembler but I rememer 
> there were non overlapping mem copy instructions that would make an array 
> copy very fast. 
> 
> Is there somthing similar in GO.
> 
> This is a general question, it is not really specific to my application. I 
> just want to know the best practices for 'extending' an array of arbitrary 
> type.

The append function should do exactly what you want, but you must use it
properly.  There is a good explanation of slices on the Go blog at
«https://blog.golang.org/go-slices-usage-and-internals».  The builtin
append function will append to the existing backing array if there is
enough capacity, but will allocate a new backing array if necessary.
Because of this, it always returns the new slice, and you must use the
returned slice, rather than the original.  Here is a playground link
demonstrating the use of append:
«https://play.golang.org/p/5Pl16hN-T5u».

...Marvin

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/20191015115219.dca3mjcdj54qxslq%40basil.wdw.

Reply via email to