Read Write files in Go

Package ioutil implements I/O functions. 1 import "ioutil" Read file: 1 config, error := ioutil.ReadFile("/etc/config") Write to file: 1 error := ioutil.WriteFile("/etc/config", config, 0644) Read direcrtory: 1 2 3 4 5 6 7 8 9 10 11 12 // ioutil.

Map in Go

Declare a map: 1 2 3 4 5 6 7 8 9 // Nil map: Read-Only var di map[string]string dict := map[string]string{ "good": "iyi", "great": "harika", "perfect": "mükemmel", } Delete an element of the map using delete builtin: 1 delete(dict, "awesome") Retrieve an value from map:

Copy in Go

Copy slice to another slice: 1 2 3 4 5 6 7 8 evens := []int{2, 4} odds := []int{3, 5, 7} N := copy(evens, odds) fmt.Printf("%d element(s) are copied.\n", N) // evens after copy is, [3, 5] // odds is intact after copy, [3, 5, 7] copy can be used over append if want to keep length of target slice intact.

Full slice expression

Simple slice expression: “[low:high]” 1 2 3 4 5 numbers := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} s := numbers[1:4] fmt.Println(s) // [1, 2, 3] fmt.Println(len(s)) // len = 3 fmt.Println(cap(s)) // cap = 9 Full slice expression:

Go errors

non-constant array bound max Array length is not a constant. slice can only be compared to nil Invalid comparison between slices. Slice can only be compared to nil.