Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report