func main() {
var stringChan chan string
fmt.Println(stringChan)
stringChan <- "hello world"
time.Sleep(time.Minute) //模擬用 讓程式暫緩退出
}
結果: 因為送不進去就會塞住bloak,等不到有其他goroutines取走資料 出現fatal error: all goroutines are asleep - deadlock!
func main() {
var stringChan chan string
// stringChan := make(chan string)
go func() {
stringChan <- "hello world"
fmt.Println("inpt channal")
}()
time.Sleep(time.Minute) //模擬用 讓程式暫緩退出
}
func main() {
var stringChan chan string
// stringChan := make(chan string)
go func() {
stringChan <- "hello world"
}()
fmt.Println(<-stringChan)
time.Sleep(time.Minute) //模擬用 讓程式暫緩退出
}
// <!-- go run main.go
// fatal error: all goroutines are asleep - deadlock!
// goroutine 1 [chan receive (nil chan)]:
// main.main()
// D:/go/src/demo-go/12-go-channel/main.go:15 +0x4e
// goroutine 6 [chan send (nil chan)]:
// main.main.func1()
// D:/go/src/demo-go/12-go-channel/main.go:12 +0x25
// created by main.main
// D:/go/src/demo-go/12-go-channel/main.go:11 +0x45
// exit status 2 -->