go语言 Goroutine与channel的配合使用

import (
	"fmt"
	"time"
)

// 定义goroutine 1
func Echo(out chan<- string) {   // 定义输出通道类型
	time.Sleep(1*time.Second)
	out <- "老铁666"
	close(out)
}

// 定义goroutine 2
func Receive(out chan<- string, in <-chan string) { // 定义输出通道类型和输入类型
	temp := <-in // 阻塞等待echo的通道的返回
	out <- temp
	close(out)
}


func main() {
	echo := make(chan string)
	receive := make(chan string)

	go Echo(echo)
	go Receive(receive, echo)

	getStr := <-receive   // 接收goroutine 2的返回

	fmt.Println(getStr)
}

猜你喜欢

转载自blog.csdn.net/weixin_44282540/article/details/115370288