交替打印数字和字母
/*
问题描述
使用两个 goroutine 交替打印序列,一个 goroutine 打印数字, 另外一个 goroutine 打印字母, 最终效果如下:
12AB34CD56EF78GH910IJ1112KL1314MN1516OP1718QR1920ST2122UV2324WX2526YZ2728
*/
func QuestionOne() {
numChan, letterChan := make(chan struct{}), make(chan struct{})
wg := sync.WaitGroup{}
go func() {
i := 1
for {
select {
case <-numChan:
fmt.Print(i)
i++
fmt.Print(i)
i++
letterChan <- struct{}{}
}
}
}()
go func(wg *sync.WaitGroup) {
i := 'A'
for {
select {
case <-letterChan:
if i >= 'Z' {
wg.Done()
return
}
fmt.Print(string(i))
i++
fmt.Print(string(i))
i++
numChan <- struct{}{}
}
}
}(&wg)
wg.Add(1)
numChan <- struct{}{}
wg.Wait()
}