问题描述
使⽤两个 goroutine 交替打印序列,⼀个 go routine 打印数字, 另外⼀
个 goroutine 打印字⺟, 最终效果如下:
12AB34CD56EF78GH910IJ1112KL1314MN1516OP1718QR1920ST2122UV2324WX2526YZ2728
解题思路
问题很简单,使⽤ channel 来控制打印的进度。使⽤两个 channel ,来分别控制数字和
字⺟的打印序列, 数字打印完成后通过 channel 通知字⺟打印, 字⺟打印完成后通知数
字打印,然后周⽽复始的⼯作。



package main
import (
"sync"
"fmt"
"strings"
)
func main() {
letter,number := make(chan bool),make(chan bool)
wait := sync.WaitGroup{}
go func() {
i:=1
for {
select {
case <- number:
fmt.Print(i)
i++
fmt.Print(i)
i++
letter <- true
break
default:
break
}
}
}()
wait.Add(1)
go func(wait *sync.WaitGroup) {
str := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
i := 0
for{
select {
case <-letter:
if i >= strings.Count(str,"")-1 {
wait.Done()
return
}
fmt.Print(str[i:i+1])
i++
if i >= strings.Count(str,"") {
i = 0
}
fmt.Print(str[i:i+1])
i++
number <- true
break
default:
break
}
}
}(&wait)
number<-true
wait.Wait()
}
go技术专栏,感兴趣者可订阅