Go语言协程和通道使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/javaxflinux/article/details/84644006

示例代码:

package main

import (
	"fmt"
	"time"
)

func hello(channel_hello chan string) {
	time.Sleep(2*time.Second)
	fmt.Println("hello.")
	//发送数据到通道
	channel_hello <- "hello_channel"
}

func main()  {
	//创建无缓冲通道channel,用于goroutine之间通信
	channel_hello := make(chan string)
	//创建协程goroutine
	go hello(channel_hello)
	//阻塞等待接收通道数据
	chan_receive := <- channel_hello
	fmt.Println("main end.", chan_receive)
}


猜你喜欢

转载自blog.csdn.net/javaxflinux/article/details/84644006
今日推荐