Analysis of Channel Usage in Go

Go Language Foundation IV

Today we are going to learn the if statement, that is, the judgment statement. Let's first look at the definition of the if statement

If definition

Conditional statements require developers to determine whether to execute the specified statement by specifying one or more conditions and testing whether the condition is true, and execute another statement when the condition is false. I believe that when readers see this, they also feel in the clouds. How can we express true and false?

Single layer if syntax format

• Conditional expression brackets can be omitted.

• With initialization statements, local variables of code blocks can be defined.

• The left bracket of the code block must be at the end of the conditional expression.

If Boolean expression{

/*Executed when Boolean expression is true*/


What we want to introduce to the reader here is that if the number given by the conditional statement program after if can be satisfied, then we express it as true; If not, return false

As shown in the code block, here we define a variable a with a value of 3, followed by an if judgment. If the value is>2, call the function to print out true; Otherwise, false is returned. In other words, we are here to judge variable a. As for the content printed out by the calling function, it is up to the reader to decide

Syntax Warning

In Go syntax, ternary operators (ternary operators) a>b? Are not supported? A: b, if readers are interested in ternary operators, they can move to java to learn about ternary operators (it is also a form of if judgment in essence)

Above is a small exercise about if judgment, which can be experienced by the reader; If the Boolean expression is true, the statement block immediately following it will be executed. If it is false, the else statement block will be executed.

In Go language, if statements also support nested processing, that is, multiple if judgments can be realized to achieve the desired results of the program

Multilayer if syntax format

As shown in the figure above, we nest another if statement in the if statement, that is, write out the judgment on the value of variable b under the default condition of a==100, and finally call the function to print out the values of a and b

Sometimes we use the Switch statement when multiple variables match the same value

Switch definition

The switch statement is used to perform different actions based on different conditions. Each case branch is unique. It is tested from top to bottom until it matches. Golang switch branch expressions can be of any type, not limited to constants. You can omit break, which is automatically terminated by default.

It can be seen from the above code block that we have defined two local variables grade and marks, and switched the marks. When the case meets different values, the values printed by calling the function are different

Type Switch

The switch statement can also be used for type switch to determine the type of variable actually stored in an interface variable

Since the purpose of the Type Switch is not very special, the author will not describe it in detail here. Readers can go to the official website to query relevant documents for learning

Select Definition

• The select statement is similar to the switch statement, but the select statement randomly executes a runnable case. If there is no case to run, it will block until there is a case to run.

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

• Select randomly executes a runnable case. If there is no case to run, it will block until there is a case to run. A default clause should always be runnable.

Precautions for Select Statement

• Each case must be a communication

• All channel expressions will be evaluated

• All expressions sent will be evaluated

• If any communication can be carried out, it will be executed; Others are ignored.

• If multiple cases can be run, Select will randomly and fairly select one to execute. Others will not be executed.

• If there is a default clause, execute the statement.

• If there is no default, select will block until a communication can run; Go does not re evaluate the channel value.

As shown in the above code, three variables, c1, c2 and c3, are defined, and the chan channel is used. Explanation on writing method: A channel that can send data of type int is generally written as channel int. According to the above syntax rules, if there is a default clause, the statement will be executed. Therefore, the execution code of the above code block is: no communication

Select Usage Supplement

• We can use select to monitor the data flow of the channel

• The use of select is very similar to the switch syntax. A new selection block starts with select, and each selection condition is described by a case statement

• The switch statement can select any condition using equality comparison. There are many restrictions on select, the biggest one of which is that each case statement must have an IO operation

• If no case is read, Go will automatically read the things described by the default statement. Under normal circumstances, each select program will have an output statement

• If no case statement is satisfied and no default statement exists, the program will enter the blocking state, and the system will issue a warning until it is cleared

Timeout judgment

According to the above code block, we have defined a select statement. In the first case, we will pass resChan to data. If the transmission time exceeds 3s, the second case statement will be executed

sign out

//In another coroutine, if the operation encounters illegal operations or unhandlable errors, it sends a data notification to shouldQuit to stop the operation


We define a shouldQuit variable of var type for the channel of the structure. First, we open a select loop, call the channel method in the case, and return the corresponding value; If not, the default value is returned. At the same time, in another coordination process, if we encounter an illegal operation or an error that cannot be handled, we will send a data notification to shouldQuit to stop running

Determine the Channel Status

Sometimes we don't like the slow cache, which is not good for us to release resources. So we can use a simple judgment method to judge: first, we open a channel of type int with a length of 5. In the channel, we open a select loop. If the value of the data channel can be received by ch, we execute the statement. Otherwise, we can discard data and other processing operations on the default statement.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us