channel的几种用法

This commit is contained in:
洪晓威 2022-01-16 18:26:27 +08:00
commit f769f2b57d
6 changed files with 191 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea

40
channel/001/main.go Normal file
View File

@ -0,0 +1,40 @@
package main
import (
"fmt"
"math/rand"
"os"
"os/signal"
"time"
)
// 两个服务一直产生数据
// 希望有数据产生就拿这些数据做一些处理
// 切换着打印从1到2
func main() {
m1, m2 := genMsg("service1"), genMsg("service2")
quit := make(chan os.Signal, 1)
go func() {
for {
fmt.Println(<-m1)
fmt.Println(<-m2)
}
}()
signal.Notify(quit, os.Interrupt)
<-quit
}
func genMsg(name string) <-chan string {
c := make(chan string)
go func() {
i := 0
for {
time.Sleep(time.Duration(rand.Intn(2000)) * time.Millisecond)
c <- fmt.Sprintf("the service name is %v,the message is %v",
name, i)
i++
}
}()
return c
}

50
channel/002/main.go Normal file
View File

@ -0,0 +1,50 @@
package main
import (
"fmt"
"math/rand"
"time"
)
// 有两个服务会产生数据
// 希望有数据就处理
// 希望不要交替执行,谁有就处理谁
// 方案1: 起一个函数这两个channel有数据就把这个数据给到另外一个channel然后读取另外一个channel
func main() {
m1, m2 := genMsg("service1"), genMsg("service2")
m := merge(m1, m2)
for{
fmt.Println(<-m)
}
}
func genMsg(name string) chan string {
c := make(chan string)
go func() {
i := 0
for {
time.Sleep(time.Duration(rand.Intn(2000)) * time.Millisecond)
c <- fmt.Sprintf("the service name is %v,the message is %v", name, i)
i++
}
}()
return c
}
// 将两个channel里的数据送到另外一个channel里
func merge(c1, c2 chan string) chan string {
c := make(chan string)
go func() {
for {
c <- <-c1
}
}()
go func() {
for {
c <- <-c2
}
}()
return c
}

51
channel/003/main.go Normal file
View File

@ -0,0 +1,51 @@
package main
import (
"fmt"
"math/rand"
"time"
)
// select
// 有两个服务都产生数据
// 有产生数据,就做处理
// 谁有数据就处理谁
// 不要用将两个channel里的数据写到新的channel里然后再读这个channel
func main() {
m1, m2 := genMsg("service1"), genMsg("service2")
m := useSelect(m1, m2)
for {
fmt.Println(<-m)
}
}
func genMsg(name string) chan string {
c := make(chan string)
go func() {
i := 0
for {
time.Sleep(time.Duration(rand.Intn(2000)) * time.Millisecond)
c <- fmt.Sprintf("the service name is %v,the message is %v", name, i)
i++
}
}()
return c
}
// 使用select监控channel
func useSelect(c1, c2 chan string) chan string {
c := make(chan string)
go func() {
for {
select {
case m := <-c1:
c <- m
case m := <-c2:
c <- m
}
}
}()
return c
}

46
channel/004/main.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"fmt"
"math/rand"
"time"
)
// 现在有n个服务每个服务都产生数据
// 产生数据就做处理
// 需要服务里有数据,就打印谁的
// 有个可变长度的 chan of string 遍历
func main() {
m1, m2, m3 := genMsg("service1"), genMsg("service2"), genMsg("service3")
m := merge(m1, m2, m3)
for {
fmt.Println(<-m)
}
}
func genMsg(name string) chan string {
c := make(chan string)
go func() {
i := 0
for {
time.Sleep(time.Duration(rand.Intn(2000)) * time.Millisecond)
c <- fmt.Sprintf("the service name is %v,the message is %v", name, i)
i++
}
}()
return c
}
func merge(chs ...chan string) chan string {
c := make(chan string)
for _, ch := range chs {
//copy := ch
go func(a chan string) {
for {
c <- <-a
}
}(ch)
}
return c
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module golanglearn
go 1.17