golang-简单的channel读写操作

Posted aguncn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang-简单的channel读写操作相关的知识,希望对你有一定的参考价值。

学了多少次了。。

应该可以实用了。

package main

import (
	"fmt"
	"time"
)

func writeChannel(c chan<- int, x int) {
	fmt.Println(x)
	c <- x
	close(c)
	fmt.Println(x)
}

func main() {
	c := make(chan int)
	go writeChannel(c, 10)
	time.Sleep(2 * time.Second)
	fmt.Println("Read: ", <-c)
	time.Sleep(2 * time.Second)
	_, ok := <-c
	if ok {
		fmt.Println("Channel is open!")
	} else {
		fmt.Println("Channel is closed!")
	}
}

  技术图片

 

以上是关于golang-简单的channel读写操作的主要内容,如果未能解决你的问题,请参考以下文章