Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the loops, judgments and selection statements in Go language

Shulou Source: shulou.com Published: 2022-06-02 01:57:19 10月05日 Update

This article shows you what the Go language cycle and judgment and selection sentences are, which are concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Learning goal

Master conditional statements, select statements, loop statements

Conditional statement if a < 5 {return 0} else {return 1}

Note:

Conditional statements do not need to use parentheses to include conditions ()

Curly braces {} must exist no matter how many statements are in the body of the statement.

Left curly braces {must be on the same line as if or else

After if, before conditional statements, you can add variable initialization statements, using the; interval

In a function with a return value, it is not allowed to include the "final" return statement in if...else... Structure, otherwise compilation will fail: function ends without a return statement.

The reason for the failure is that the Go compiler cannot find the return statement that terminates the function.

Examples of compilation failures are as follows:

Func example (x int) int {if x = = 0 {return 5} else {return x / / cannot find the return statement that terminates the function}} Select statement switch I {/ / I can only be constant or integer case 0: fmt.Printf ("0") case 1: Fmt.Printf ("1") case 2: fallthrough / / execute case 3: fmt.Printf ("3") case 4 5,6: / / I equals 4 | 5 | fmt.Printf ("4,5,6") default: / / other fmt.Printf ("Default")} / / switch is executed at 6 or the expression: switch {case 0 can be left unset.

Tags: Statements loops conditions selection parentheses support languages functions expressions compilation one line keywords content constants skills integers knowledge structure conciseness Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info vpn Microsoft MySQL