Golang Collections Intro
Posted on August 16, 2020
(Last modified on August 23, 2020)
| 1 minutes
| Kinshuk Chandra
Golang has two main collection types.
maps.
arrays and slices
Pass slice as a function argument
Posted on August 16, 2020
(Last modified on August 27, 2020)
| 2 minutes
| Kinshuk Chandra
In Go, the function parameters are passed by value. With respect to use slice as a function argument, that means the function will get the copies of the slice: a pointer which points to the starting address of the underlying array, accompanied by the length and capacity of the slice. Oh boy! Since you know the address of the memory which is used to store the data, you can tweak the slice now.
[Read More]Prepend in Slice
Posted on August 16, 2020
(Last modified on August 27, 2020)
| 1 minutes
| Kinshuk Chandra
Go has a built-in append function which add elements in the slice:
func append(slice []Type, elems …Type) []Type
But how if we want to the “prepend” effect? Maybe we should use copy function. E.g.:
package main import "fmt" func main() { var s []int = []int{1, 2} fmt.Println(s) s1 := make([]int, len(s) + 1) s1[0] = 0 copy(s1[1:], s) s = s1 fmt.Println(s) } The result is like this:
[1 2] [0 1 2] But the above code looks ugly and cumbersome, so an elegant implementation maybe here:
[Read More]Reallocating underlying array of slice
Posted on August 16, 2020
(Last modified on August 27, 2020)
| 5 minutes
| Kinshuk Chandra
When appending data into slice, if the underlying array of the slice doesn’t have enough space, a new array will be allocated. Then the elements in old array will be copied into this new memory, accompanied with adding new data behind. So when using Go built-in append function, you must always keep the idea that “the array may have been changed” in mind, and be very careful about it, otherwise, it may bite you!
[Read More]Slice vs Arrays
Posted on August 16, 2020
(Last modified on August 23, 2020)
| 1 minutes
| Kinshuk Chandra
Performance Slice operations are cheap! Slicing: Creates a new slice header. Assigning a Slice to another Slice or, passing it to a function: Only copies the slice header. Slice header has a fixed size and it doesn't change even if we have got millions of elements. Array can be expensive as compared to Slice. Assigning an array to another array or passing it to a function: Copies all the elements of it.
[Read More]The internals of slice
Posted on August 16, 2020
(Last modified on August 27, 2020)
| 3 minutes
| Kinshuk Chandra
There are 3 components of slice:
a) Pointer: Points to the start position of slice in the underlying array;
b) length (type is int): the number of the valid elements of the slice;
b) capacity (type is int): the total number of slots of the slice.
Check the following code:
package main import ( "fmt" "unsafe" ) func main() { var s1 []int fmt.Println(unsafe.Sizeof(s1)) } The result is 24 on my 64-bit system (The pointer and int both occupy 8 bytes).
[Read More]Two-dimensional slice
Posted on August 16, 2020
(Last modified on August 27, 2020)
| 3 minutes
| Kinshuk Chandra
Go supports multiple-dimensional slice, but I only want to introduce two-dimensional slice here. One reason is the two-dimensional slice is usually used in daily life, while multiple-dimensional seems not common. If you often use multiple-dimensional slice, personally I think the code is a little clumsy and not easy to maintain, so maybe you can try to check whether there is a better method; the other reason is the principle behind multiple-dimensional slice is the same with two-dimensional slice, you can also understand it if you know two-dimensional slice well.
[Read More]