2.22. Go language select statement

发布时间 : 2025-10-25 13:32:55 UTC      

Page Views: 10 views

select is a control structure in Go , similar to the one used for communication switch statement. Each case must be a communication operation, either sending or receiving.

select randomly execute a runnable case . If not, case runnable, it will block until there is case can be run. A default clause should always run.

2.22.1. Grammar #

The syntax of the select statement in the Go programming language is as follows:

select { case communication clause : statement(s); case communication clause : statement(s); /* You can define any number of cases */ default : /* optional */ statement(s); } 

The following description describes select syntax of the statement:

  • Each case must be a communication.

  • All channel expressions are evaluated.

  • All expressions sent will be evaluated

  • If any communication can be performed, it is executed and the others are ignored.

  • If there are more than one case can be run. Select will be randomly and fairly selected for execution. The rest will not be carried out. Otherwise:

    1. If there is default clause, the statement is executed.

    2. If there is no default clause, select will block until a communication can run; Go will not re evaluate channel or values.

2.22.2. Example #

select statement application demonstration:

Example #

package main import "fmt" func main() { var c1, c2, c3 chan int var i1, i2 int select { case i1 = <- < span>c1: fmt.Printf("received ", i1, " from c1\\n") case c2 <- < span> i2: fmt.Printf("sent ", i2, " to c2\\n") case i3, ok := (<- < span>c3): // same as: i3, ok := <- < span>c3 if ok { fmt.Printf("received ", i3, " from c3\\n") } else { fmt.Printf("c3 is closed\\n") } default: fmt.Printf("no communication\\n") } }    

The result of the above code execution is:

no communication 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。