您的当前位置:首页正文

go交替打印数字和字符

2024-12-01 来源:个人技术集锦
package main

import "fmt"

func main() {
	A := make(chan bool,1)
	B := make(chan bool)
	Exit := make(chan bool)
	go func() {
		s := []int{1, 2, 3, 4}
		for i := 0; i < len(s) ; i++  {
			if ok := <-A; ok {
				fmt.Println("A:", s[i])
				B <- true
			}
		}
	}()
	go func() {
		defer func() {
			close(Exit)
		}()
		s := []byte{'A', 'B', 'C', 'D'}
		for i := 0; i < len(s); i++ {
			if ok := <- B; ok {
				fmt.Printf("B: %c\n", s[i])
				A <- true
			}
		}

	}()
	A <- true
	<- Exit
}
显示全文