From 57c0d5dff81cc936898cc9db1e89da0ee64bee5a Mon Sep 17 00:00:00 2001 From: Jian Han Date: Fri, 27 Apr 2018 22:29:19 +1000 Subject: [PATCH] hello world with job example --- master_concurrent_go/ch01/main.go | 33 +++++++++++++++++++++++ the_go_programming_language/ch04/slice.go | 20 ++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 master_concurrent_go/ch01/main.go diff --git a/master_concurrent_go/ch01/main.go b/master_concurrent_go/ch01/main.go new file mode 100644 index 0000000..8d806b9 --- /dev/null +++ b/master_concurrent_go/ch01/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "time" +) + +type Job struct { + i int + max int + text string +} + +func outputText(j *Job) { + for j.i < j.max { + time.Sleep(1 * time.Millisecond) + fmt.Println(j.text) + j.i++ + } +} + +func main() { + hello := new(Job) + world := new(Job) + hello.text = "hello" + hello.i = 0 + hello.max = 3 + world.text = "world" + world.i = 0 + world.max = 5 + go outputText(hello) + outputText(world) +} diff --git a/the_go_programming_language/ch04/slice.go b/the_go_programming_language/ch04/slice.go index ac0495a..2ffda0f 100644 --- a/the_go_programming_language/ch04/slice.go +++ b/the_go_programming_language/ch04/slice.go @@ -12,3 +12,23 @@ func main() { m2[0] = "Feb updated" fmt.Print(m1, m2) } + +func appendInt(x []int, y int) []int { + var z []int + zlen := len(x) + 1 + if zlen <= cap(x) { + // There is room to grow. Extend the slice. + z = x[:zlen] + } else { + // There is insufficient space. Allocate a new array. + // Grow by doubling, for amortized linear complexity. + zcap := zlen + if zcap < 2*len(x) { + zcap = 2 * len(x) + } + z = make([]int, zlen, zcap) + copy(z, x) // a built-in function; see text + } + z[len(x)] = y + return z +}