Hi
I like GO language so i'm trying to learn this language.
Using a slice in a little piece of example that append space in a []byte
the result of capacity of slice varying in function of number of appends.
Is there an algorithm to determine the cap?
I found ... the cap increase by step of 2^n when the cap of bytes is
insufficient
when i have added 33 elements the cap of slice is 64 so i have a slice
double
I think that when increasing the number of elements growing the bytes
allocated but not used.
example:
readerString:=strings.NewReader(" ")
onebyte,_:=readerString.ReadByte()
testing:=[]byte{}
fmt.Printf("n° byte before append>testing len(%d) cap(%d) %v
\n",len(testing),cap(testing),testing)
for i:=0 ;i<10; i++ {
testing=append(testing, onebyte)
fmt.Printf("\tn° byte after append %d >testing len(%d) cap(%d) %v
\n",i,len(testing),cap(testing),testing)
}
result:
n° byte before append>testing len(0) cap(0) []
n° byte after append 0 >testing len(1) cap(1) [32]
n° byte after append 1 >testing len(2) cap(2) [32 32]
n° byte after append 2 >testing len(3) cap(4) [32 32 32]
n° byte after append 3 >testing len(4) cap(4) [32 32 32 32]
n° byte after append 4 >testing len(5) cap(8) [32 32 32 32 32]
n° byte after append 5 >testing len(6) cap(8) [32 32 32 32 32 32]
n° byte after append 6 >testing len(7) cap(8) [32 32 32 32 32 32 32]
n° byte after append 7 >testing len(8) cap(8) [32 32 32 32 32 32 32 32]
n° byte after append 8 >testing len(9) cap(16) [32 32 32 32 32 32 32 32 32]
n° byte after append 9 >testing len(10) cap(16) [32 32 32 32 32 32 32 32 32
32]
--