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]

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]

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]

Reallocating underlying array of slice

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]