mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 12:46:03 +03:00
hello world with job example
This commit is contained in:
parent
2ca35c8593
commit
57c0d5dff8
33
master_concurrent_go/ch01/main.go
Normal file
33
master_concurrent_go/ch01/main.go
Normal 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)
|
||||||
|
}
|
@ -12,3 +12,23 @@ func main() {
|
|||||||
m2[0] = "Feb updated"
|
m2[0] = "Feb updated"
|
||||||
fmt.Print(m1, m2)
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user