This is a creation in Article, where the information may have evolved or changed.
The features of go in Process Control are as follows:
- There is no do and while loops, only one generalized for statement
- Switch statements are flexible and can also be used for type judgments
- Both the IF statement and the switch statement can contain an initialization child statement
- The break statement and the continue statement can be followed by a label label statement that identifies the block of code that needs to be terminated or resumed
- Defer statements make it easier to perform exception capture and resource reclamation tasks
- Select statements can also be used for multi-branch selection, but only with channels
- The GO statement is used to start goroutine asynchronously and execute the specified function
For range Note points:
- Iterate over an array, slice, or string value: = When there is only one iteration variable on the left, be aware that you can only get the index of the element, not the element.
- Iterations do not have an array value of any element, a slice value of nil, a dictionary value of nil, a string value of "", and the code in the For statement is not executed. For the first, it will end. Because these value lengths are 0
- A channel value that iterates over nil causes the current process to block forever on the for statement.
Defer control statements
Package Mainimport ("FMT")//Outerfunc is a peripheral function//defer execution order is, advanced post-out, deferred invocation of the specified function//when the statement executes in the perimeter function, only the deferred function is executed, and the peripheral function really ends//when you execute return in the foreign media function, only the delay function is executed, and the peripheral function returns//When code in the perimeter function throws a run-time error, the runtime error is spread to the calling function only if the deferred function finishes executing. //so defer is often used to perform the finishing touches such as releasing resources or exception handling//defer statement in the peripheral function body position Unlimited, unlimited number offunc printnumbers () { forI: =0; I <5; i++ { //defer is pressed into a stack, the function is finished before the moment, advanced out, so is 43210defer func () {fmt. Printf ("%d", I)} ()}}func printNumbers1 () { forI: =0; I <5; i++ { //defer is pressed into a stack, the function is finished before the moment, advanced out, so is 43210Defer func (nint) {fmt. Printf ("%d", N)} (i)}}func printNumbers2 () { forI: =0; I <5; i++ { //defer is pressed into a stack, the function is finished before the moment, advanced out, so is 43210Defer func (nint) {fmt. Printf ("%d", N)} (I*2)}}func Main () {printnumbers () fmt. Println () printNumbers1 () fmt. Println () printNumbers2 ()}