Go - for ... range

Intro for ... range clause can be used to iterate 5 types of variables: array, slice, string, map and channel, and the following sheet gives a summary of the items of for ... range loops: Type 1st Item 2nd Item Array index value Slice index value String index (rune) value (rune) Map key value Channel value Example - Iterating on slice package main import "fmt" func main() { s := [] {1,2,3} for i,v := range s { fmt. [Read More]

Golang Slices

Intro A slice in Golang is an abstraction that sits on top of an array. An array normally has a set size and must be initialised with a size. A slice is a little more like an array in javascript or an ArrayList in Java. A slice is a descriptor of an array segment. It consists of a pointer to the array, the length of the segment, and its capacity (the maximum length of the segment). [Read More]

"nil slice" vs "nil map"

Slice and map are all reference types in Go, and their default values are nil: package main import "fmt" func main() { var ( s []int m map[int]bool ) if s == nil { fmt.Println("The value of s is nil") } if m == nil { fmt.Println("The value of m is nil") } } The result is like this: The value of s is nil The value of m is nil When a slice’s value is nil, you could also do operations on it, such as append: [Read More]

Conversion between array and slice

In Go, array is a fixed length of continuous memory with specified type, while slice is just a reference which points to an underlying array. Since they are different types, they can’t assign value each other directly. See the following example: package main import "fmt" func main() { s := []int{1, 2, 3} var a [3]int fmt.Println(copy(a, s)) } Because copy only accepts slice argument, we can use the [:] to create a slice from array. [Read More]

Go - Slice copy

The definition of built-in copy function is here: func copy(dst, src []Type) int The copy built-in function copies elements from a source slice into a destination slice. (As a special case, it also will copy bytes from a string to a slice of bytes.) The source and destination may overlap. Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst). Let’s see a basic example in which source and destination slices aren’t overlapped: [Read More]

Pass slice as a function argument

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

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]

The internals of slice

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

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]