channel 先读后写的情况是怎么处理的

package main

import (
	"fmt"
	"time"
)

func main() {

	mailbox := make(chan string, 10)
	//mailbox := make(chan string)
	mail, ok := <-mailbox
	fmt.Println(mail, ok)

	go func() {

		mailbox <- "letter"
	}()

	time.Sleep(5 * time.Second)

}
`

很多channel的例子都是先写个数据,再读数据,这样运行正常,但是实际场景中 读写的顺序是未知的,也可能是先读,后写入,但是这样会提示 fatal error: all goroutines are asleep - deadlock!

共 0 个回复