1.向已经关闭的通道发送值,引起panic错误
package main import "fmt" func recv(c chan int) { ret := <-c fmt.Println("接收成功", ret) } func main() { ch := make(chan int) go recv(ch) // 启用goroutine从通道接收值 ch <- 10 close(ch) ch <- 9 fmt.Println("发送成功") }
2.首先向通道发送值,然后在启动接受通道值的协程,引起deadlock错误
package main import "fmt" func recv(c chan int) { ret := <-c fmt.Println("接收成功", ret) } func main() { ch := make(chan int) ch <- 10 go recv(ch) // 启用goroutine从通道接收值 fmt.Println("发送成功") }
3.向没有接受者的无缓存的通道发送值,引起deadlock错误
package main import "fmt" func main() { ch := make(chan int) ch <- 10 fmt.Println("发送成功") }
4.关闭已经关闭的通道,引起panic错误
package main import "fmt" func recv(c chan int) { ret := <-c fmt.Println("接收成功", ret) } func main() { ch := make(chan int) go recv(ch) // 启用goroutine从通道接收值 ch <- 10 close(ch) close(ch) fmt.Println("发送成功") }
标签:int,fmt,chan,例子,func,import,main,channel,通道 From: https://www.cnblogs.com/zxqblogrecord/p/17753969.html