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.

On Tuesday, 15 October 2019 11:16:35 UTC+1, Stuart Davies wrote:
>
> Hi 
>
> I have a slice backed by an array. 
>
> My understanding is that I can extend the slice (append)  up to the 
> capacity of the array. 
>
> After that I do not understand the correct procedure. Append fails when 
> the backing array indexes are exceeded.
>
> So I have this temporary piece of code:
> if p.LineCount >= len(p.Offsets) {
>     newLen := p.LineCount + 50
>     sb := make([]int64, newLen)
>     for i := 0; i < p.LineCount; i++ {
>         sb[i] = p.Offsets[i]
>     }
>     p.Offsets = sb
> }
>
> I know there must be an array copy function that will do this more 
> efficiently but I failed to find it!
>
> Please help.
>
>
>

-- 
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/d50b9df8-121f-4f92-811d-463ec92776b0%40googlegroups.com.

Reply via email to