Input Scanning using bufio in Go

Read standard input by line: import ( "bufio" "os" "fmt" ) func main() { // New scanner to read from stdin. // By default it reads line. ie. split // function defaults to ScanLines. in := bufio.NewScanner(os.Stdin) for in.Scan() { // Read line and print it. fmt.Println(in.Text()) } } Read by words: // Set split function to ScanWords before Scan() function. in.Split(bufio.ScanWords) for in.Scan() { . [Read More]
go  note 

1% Better Every Day by James Clear

My notes of 1% Better Every Day - James Clear at ConvertKit Craft + Commerce 2017 talk. The aggregation of marginal gains. To explain this, speaker talks about bicycle group example that work on small improvement to achieve an goal. Improve 1% each day that compound to end up 37% times better at end of year. Habits are compound interest of self-improvement. Framework of building good habits. 4 stages of habit formation: [Read More]

Read Write files in Go

Package ioutil implements I/O functions.

import "ioutil"

Read file:

config, error := ioutil.ReadFile("/etc/config")

Write to file:

error := ioutil.WriteFile("/etc/config", config, 0644)

Read direcrtory:

// ioutil.ReadDir returns slice of os.FileInfo
files, error := ioutil.ReadDir("/home/akshay")

if error != nil {
    // Handle error
}

for _, file := range files {
    // file.Size()
    // file.Name()
    // file.IsDir()
}

For more functions, read ioutil doc.

go  note 

Map in Go

Declare a map:

// 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:

delete(dict, "awesome")

Retrieve an value from map:

if value, ok := dict["great"]; ok {
    fmt.Printf("%q found in dict", value)
}

Iterate through map elements:

for key, value := range dict {
    fmt.Println(key, value)
}
go  note 

Copy in Go

Copy slice to another slice: 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. If the source slice is bigger in size than destination, copy only copies slice elements that occupies in target slice. [Read More]
go  note 

Full slice expression

Simple slice expression: “[low:high]” 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: “[low:high:max]” numbers := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} s := numbers[1:4:5] fmt.Println(s) // [1, 2, 3] fmt.Println(len(s)) // len = 3 fmt.Println(cap(s)) // cap = 4 Full slice expression can be used for limit backing array sharing. [Read More]
go  note 

Go errors

  1. non-constant array bound max

Array length is not a constant.

  1. slice can only be compared to nil

Invalid comparison between slices. Slice can only be compared to nil.

go  note 

Slice append function in Go

Append an element to the slice:

nums := []int{1, 2, 3}
nums = append(nums, 4)

Append multiple elements to the slice:

nums := []int{1, 2}
nums = append(nums, 3, 4)

Append slice to another slice:

nums := []int{1, 2}
tens := []int{11, 12}
nums = append(nums, tens...)

// nums is {1, 2, 11, 12}
go  note 

Keyed array elements in Go

Unkeyed array elements: example := [3]float64{ 1.3, 6.7, 4.0, } Keyed array elements: example := [3]float64{ 0: 1.3, 1: 6.7, 2: 4.0, } Keyed array in different order: example := [3]float64{ 1: 1.3, 0: 6.7, 2: 4.0, } Auto-initialize keyed array elements: example := [...]float64{ // index 0 is empty // index 1 is empty 2: 4.0, // index 2 } Unkeyed and keyed array elements: example := [. [Read More]
go  note 

break and continue labels

break and continue statements can be use with label in Go, similar to goto statement. It is optional. Scope of labels are limited to the function and does not conflict with variable name, as it live in separate space. break and continue with label is only used in for, switch and select statements. OuterLoop: for i := 0; i < 3; i++ { for j := 0; j < 3; j++ { fmt. [Read More]
go  note