其他分享
首页 > 其他分享> > go 交替打印数字和字母(步长2字符)

go 交替打印数字和字母(步长2字符)

作者:互联网

func main() {
	wg := sync.WaitGroup{}
	wg.Add(1)
	num := 28
	a := make(chan int)
	b := make(chan int)
	go func() {
		defer wg.Done()
		for i := 1; i < num; {
			<-a
			fmt.Println(i)
			i++
			fmt.Println(i)
			i++
			b <- 1

		}
	}()
	go func() {
		defer wg.Done()
		for i := 'a'; i < 'z'; {
			<-b
			fmt.Printf("%c", i)
			i++
			fmt.Printf("%c", i)
			i++
			a <- 1
		}
		<-b
	}()
	a <- 1
	wg.Wait()
	close(a)
	close(b)

输出 12AB34CD56EF78GH910IJ1112KL1314MN1516OP1718QR1920ST2122UV2324WX2526YZ

参考:Go语言面试题:交替打印数字和字母 - 知乎

标签:wg,字符,int,make,chan,步长,num,go
来源: https://blog.csdn.net/weixin_50255436/article/details/123118377