2个协程交替打印字母和数字
答案1:
package mainimport ("fmt")func main() {limit := 26numChan := make(chan int, 1)charChan := make(chan int, 1)mainChan := make(chan int, 1)charChan <- 1go func() {for i := 0; i < limit; i++ {<-charChanfmt.Printf("%c\n", 'a'+i)numChan <- 1}}()go func() {for i := 0; i < limit; i++ {<-numChanfmt.Println(i)charChan <- 1}mainChan <- 1}()<-mainChanclose(charChan)close(numChan)close(mainChan)}
