Just sharing some tips.

When working with slices, it is often a good idea to lend a helping hand to 
the compiler. 

Don't declare something like this, unless you have no other choices.
```
var slice []int
```
If you know the data right away, you can do something like this:
```
slice := []int{1,2,3,4,5}
```
If you don't know the data, but you know the size, you can do something 
like this:
```
slice:=make([]int, 5)

//example: working with slice
for a:=0; a<5; a++ {
   slice[a] = a+1
}
```
If you don't know the data and the size, you can do something like this:
```
slice:=make([]int, 0, 5) 
//make a slice with 0 length, but allocate memory for 5 items (capacity of 
5).
//The capacity can be of any arbitrary length. Think of it like a 
suggestion of what you think the data size may be.

//example
for a:=0; a<10; a++ {
   slice = append(slice, a+1) 
   //As long as slice length does not exceed its capacity, no new slice is 
allocated.
   //If the length exceed the capacity, the compiler will allocate twice 
the original capacity or at least enough to store the items (whichever is 
larger).
   //In this case, asuming only one item is appended at a time, append will 
create new slices with the following capacity: 5, 10, 20, 40, ...
)
```
Now let's see what happens when you do this:
```
var slice []int

for a:=0; a<10; a++ {
   slice = append(slice, a+1)
   //When a=0, the slice has 0 capacity but need 1, hence a new slice is 
created with 1 capacity and 1 length. 
   //When a=1, the slice has 1 capacity but need 2, hence a new slice is 
created with 2 capacity (twice the original capacity: 2x1) and 2 length.
   //When a=2, the slice has 2 capacity but need 3, hence a new slice is 
created with 4 capacity (twice the original capacity: 2x2) and 3 length.
   //When a=3, the slice has 4 capacity and need 4, hence no new slice is 
created and the existing slice is returned with 4 length.
   //When a=4, the slice has 4 capacity but need 5, hence a new slice is 
created with 8 capacity (twice the original capacity: 2x4) and 5 length.
   //When a=5, the slice has 8 capacity and need 6, hence no new slice is 
created and the existing slice is returned with 6 length.
   //etc.
}
```
If you do not specify the length or the capacity, you are bound to create 
several unnecessary memory allocations. It may not matter much in a real 
world application, but I think it is a good idea to help out the compiler 
as long as it is reasonable to do so, and specifying the length or the 
capacity does not take much effort on your part. 

On Monday, August 9, 2021 at 12:25:41 PM UTC+7 Miraddo wrote:

> Thanks, Brain. It is absolutely fantastic. I saw this SliceTricks two days 
> ago. Nice to mention it.
>
> On Sunday, August 8, 2021 at 8:36:27 PM UTC+4:30 Brian Candler wrote:
>
>> On Friday, 6 August 2021 at 18:00:35 UTC+1 Konstantin Khomoutov wrote:
>>
>>> Have you read and understood [1] and [2]? 
>>>
>> ...
>>
>>
>>> 1. https://blog.golang.org/slices-intro 
>>> 2. https://blog.golang.org/slices 
>>>
>>>
>> Also possibly of interest (although it doesn't mention rotation):
>> https://github.com/golang/go/wiki/SliceTricks 
>>
>

-- 
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/969cf55b-c432-4f6a-b0ae-552b6f8f6991n%40googlegroups.com.

Reply via email to