-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_goroutine及channel题.go
76 lines (71 loc) · 1.34 KB
/
11_goroutine及channel题.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* @Author: your name
* @Date: 2021-01-16 21:42:50
* @LastEditTime: 2021-01-21 17:56:58
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /demo/11_goroutine及channel题.go
*/
/**
1. 使用goroutine随机生成int64数据
2. 计算数据的各个位数的和
3. 打印这些数据在主函数
*/
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
var ranOnce sync.Once
var ranWait sync.WaitGroup
type ranst struct {
random int64
count int64
}
func createRandom(cnRdm chan<- int64) {
// defer ranWait.Done()
rand.Seed(time.Nanosecond.Nanoseconds())
for i := 0; i < 100; i++ {
cnRdm <- rand.Int63n(10000)
}
close(cnRdm)
}
func consumerAndCount(cn <-chan int64, cnCont chan<- ranst) {
// defer ranWait.Done()
var (
random int64
save int64
)
for i := 0; i < 100; i++ {
random = <-cn
save = random
count := int64(0)
for random > 0 {
count += random % 10
random = random / 10
}
cnCont <- ranst{
random: save,
count: count,
}
}
close(cnCont)
}
func ranCountMain() {
var (
cnRdm = make(chan int64, 100)
cnCont = make(chan ranst, 100)
)
ranWait.Add(2)
go createRandom(cnRdm)
go consumerAndCount(cnRdm, cnCont)
for val := range cnCont {
fmt.Printf("%d计算的位数是:%d\n", val.random, val.count)
}
// ranWait.Wait()
}
func initrsd() {
ranCountMain()
}