Go 编程语言中 select 语句的语法如下 -
以下规则适用于 select 语句 -
- select 中可以有任意数量的 case 语句。每个 case 后跟 to be compare to 的值和一个冒号。
- case 的类型必须是 a communication channel 操作。
- 当 channel 操作发生时,将执行该情况之后的语句。case 语句中不需要中断。
- select 语句可以具有可选的 default case,该大小写必须出现在 select 的末尾。当所有 case 都不为 true 时,默认 case 可用于执行任务。默认情况下不需要中断。
例
编译并执行上述代码时,它会产生以下结果——
no communication