hello world with job example

This commit is contained in:
Jian Han 2018-04-27 22:29:19 +10:00
parent 2ca35c8593
commit 57c0d5dff8
2 changed files with 53 additions and 0 deletions

View File

@ -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)
}

View File

@ -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
}