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]
Go - Maps
Intro Maps are somewhat similar to what other languages call “dictionaries” or “hashes”. A map maps keys to values.
Here are some points to look at:
Maps allows us to quickly access to an element/value using a unique key. Map keys must be unique because otherwise it can’t find the corresponding values/elements. The types of Map Keys and Values in Maps can be different. A Map Key must be a comparable type.
[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]
Go - Array
Syntax In Go, the length is also a part of array type. So the following code declares an array:
var array [3]int while “var slice []int” defines a slice. Arrays cannot be resized.
Another way is to set the array entries as you declare the array:
func main() { a := [2]string{"hello", "world!"} fmt.Printf("%q", a) } You can also use an implicit length when passing values to array using [...]:
[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]
Accessing Map
Map is a reference type which points to a hash table, and you can use it to construct a “key-value” database which is very handy in practice programming. E.g., the following code will calculate the count of every element in a slice:
package main import ( "fmt" ) func main() { s := []int{1, 1, 2, 2, 3, 3, 3} m := make(map[int]int) for _, v := range s { m[v]++ } for key, value := range m { fmt.
[Read More]
Adding multiple types to collection
I would generally recommend avoiding this, but sometimes it could be neccessary. To add multiple types to a single map you must declare the map to take the interface type. The interface type is comparable to Object in Java. Everyhing is an interface.
package main
import "fmt"
func main(){
m := map[string]interface{}{
"test":1,
"test2":"test",
}
fmt.Println(m)
s := []interface{}{
"test",
2,
}
fmt.Println(s)
}
Adding sugar - map, reduce and filter, etc to slice
Golang prides itself on being a simple , pragmatic language and tried to avoid sugar that its creators feel are unnecessary. So lets add some map reduce functions ourselves to see what is involved:
package main import "fmt" type MyList []string // a custom type can be a builtin type if you want it to be func (ml MyList) Filter(f func(string) bool) []string { na := []string{} for _, v := range ml { if add := f(v); add { na = append(na, v) } } return na } func (ml MyList) Reduce(f func(prev, current string, index int) string) []string { na := []string{} for i, v := range ml { if i == 0 { na = append(na, f("", v, i)) } else { na = append(na, f(ml[i-1], v, i)) } } return na } func (ml MyList) Map(f func(val string) *string) []string { na := []string{} for _, v := range ml { mVal := f(v) if nil !
[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]