设计模式 - 传输对象模式
Go Routine Practice - tcp server&client

Go Channel Practice V - 祖安萝莉

violet posted @ 5 年前 in 笔记 with tags Golang GoRoutine Channel , 10910 阅读

idea 来自 https://www.zhihu.com/question/402635037/answer/1302122540

逻辑没有照抄,只是做 channel 使用方法练习。

 

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
 
import (
    "bufio"
    "fmt"
    "math/rand"
    "os"
    "sync"
    "time"
)
 
var universal = []string{
    "你说你马呢?",
    "那没事了。",
    "真别逗我笑啊。",
    "那可真是有趣呢。",
    "就这?就这?",
    "你品,你细品。",
    "不会真有人觉得是这样的吧,不会吧不会吧?",
    "你在教我做事?",
}
 
var strong = []string{
    "你急了急了急了?",
    "他急了,他急了!",
}
 
var question = []string{
    "不会真有人还不知道吧?",
    "你都不知道,那你说你马呢?",
    "说你马呢?",
}
 
var questionMap = map[string]bool{
    "不会真有人还不知道吧?":   true,
    "你都不知道,那你说你马呢?": true,
    "说你马呢?":         true,
}
 
var strongExpressionMap = map[string]bool{
    "!":   true,
    "???": true,
    "???": true,
    "气抖冷": true,
}
 
var expressionMap = map[string]bool{
    "?"true,
    "怎么": true,
    "什么": true,
    "咋"true,
    "?"true,
}
 
func main() {
    scanner := bufio.NewScanner(os.Stdin)
    var wg sync.WaitGroup
 
    output := make(chan string, 40)
    quit := make(chan os.Signal)
 
    go collectOutput(output, quit)
 
    fmt.Print(">>>: ")
    for scanner.Scan() {
        input := scanner.Text()
        wg.Add(1)
        go func(input string) {
            defer wg.Done()
 
            parseInput(input, output)
        }(input)
 
    }
 
    wg.Wait()
 
    if scanner.Err() != nil {
        panic("scan error")
    }
}
 
func parseInput(input string, output chan string) {
    rand.Seed(time.Now().UnixNano())
    if _, ok := questionMap[input]; ok {
        output <- universal[rand.Intn(len(universal))]
        return
    }
    if _, ok := expressionMap[input]; ok {
        output <- universal[rand.Intn(len(universal))]
        return
    }
    if _, ok := strongExpressionMap[input]; ok {
        output <- strong[rand.Intn(len(strong))]
        return
    }
    output <- universal[rand.Intn(len(universal))]
}
 
func collectOutput(output chan string, quit chan os.Signal) {
    for {
        select {
        case o := <-output:
            time.Sleep(time.Duration(rand.Intn(2)) * time.Second)
            fmt.Println("你马bot: ", o)
            fmt.Print(">>>: ")
        case <-quit:
            return
        }
    }
}

Vip Delhi Call GIrls 说:
3 年前

This is one of the best courses where there can be lots of great and nice activities that will be performed out here.

192.168.1.1 Router L 说:
3 年前

Do you have a spam issue on this website; I likewise am a blogger, and I was pondering your circumstance; we have fostered a few pleasant techniques and we are hoping to trade strategies with others, why not give me an email whenever intrigued.

AP SSC Urdu Question 说:
3 年前

Urdu is one of the main languages in the state, and this is the first language for Urdu Medium students, there are fewer schools are working in all districts of the state, all the applicable students also can download AP SSC Urdu Model Paper 2023 Pdf in chapter wise for all lessons of the course, AP SSC Urdu Question Paper download, and practice the Ibtedai Question bank to get better rank in all exams conducted by BSEAP. Urdu is one of the main languages in the state, and this is the first language for Urdu Medium students, there are fewer schools are working in all districts of the state, all the applicable students also can download AP SSC Urdu Model Paper 2023 Pdf in chapter wise for all lessons of the course.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter