This is a creation in Article, where the information may have evolved or changed. This is the 8th chapter of our [Golang Series Tutorial] (/SUBJECT/2). If is a conditional statement. The syntax for the IF statement is ' goif condition {} ' if ' condition ' is true, then the code between ' {' and '} ' is executed. Unlike other languages, such as the C language, the ' {} ' in the Go language is necessary, even if there is only one statement between ' {} '. The IF statement also has an optional ' else if ' and ' Else ' section. There can be any number of ' else if ' between goif condition {} else if condition {} else {} ' if-else statements. The order of conditional judgments is from top to bottom. If the ' if ' or ' else if ' condition determines that the result is true, the corresponding code block is executed. If no condition is true, then the ' Else ' code block is executed. Let's write a simple program to detect whether a number is odd or even. "' Gopackage mainimport (" FMT ") func main () {num: = if num% 2 = = 0 {//checks If number is even fmt. Println ("The number is even")} else {fmt. Println ("The number is odd")}} "[Run Program Online] (https://play.golang.org/p/vWfN8UqZUr) ' if num%2 = = 0 ' statement detects whether the remainder of NUM Fetch 2 is zero. If it is zero then the printout "the number is even", if not zero, prints out "the number is odd". In the above program, the printout is ' the number is even '. The ' if ' has another form, which contains a ' statement ' optional statement part that runs before the condition is judged. Its syntax is ' ' ' goif statement; Condition {} "let's rewrite the program and use the syntax above to find whether the number is even or odd." "' Gopackage mainimport (" FMT ") func main () {if num: = ten; num% 2 = = 0 {//checks If number IS even FMT. PRINTLN (num, "is even")} else {fmt. PRINTLN (num, "is Odd")}} "[Online Run Program] (HTTPS://PLAY.GOLANG.ORG/P/_X9Q4MWR4S) in the above program, ' Num ' is initialized in ' if ' statement, ' num ' can only be from ' if ' and ' Else ' access. That means ' num ' is limited to the ' if ' Else ' code block. If we try to access ' num ' from other external ' if ' or ' else ', the compiler will not pass. Let's write another program that uses ' else if '. "' Gopackage mainimport (" FMT ") func main () {num: = if num <= (FMT). Println ("number is less than or equal")} else if num >= && num <= {fmt. Println ("number is between and")} else {fmt. Println ("number is greater than 100")}} "[Running Program Online] (Https://play.golang.org/p/Eji7vmb17Q) in the above program if ' else if Num >= && num <= 100 ' is true, the program will output ' number is between and 100 '. [Get free Golang tools] (HTTPS://APP.MAILERLITE.COM/WEBFORMS/POPUP/P8T5T8) # # # A note that the ' Else ' statement should be in the same row after the curly brace '} ' of the ' if ' statement. If not, the compiler will not pass. Let's use the following procedure to understand it. "' Gopackage mainimport (" FMT ") func main () {num: = if num% 2 = = 0 {//checks If number is even fmt. Println ("The number is even")} else {fmt. Println ("the Number is odd ")}}" [Running Program Online] (https://play.golang.org/p/RYNqZZO2F9) in the above program, the ' Else ' statement does not start with the same line as the ' if ' statement after the end of '} '. Instead, start with the next line. This is not allowed. If you run this program, the compiler will output an error, "' Main.go:12:5: Syntax error:unexpected else, expecting} ' is an error because the semicolon of the go language is automatically inserted. You can read the semicolon insertion rule [https://golang.org/ref/spec#Semicolons] (https://golang.org/ref/spec#Semicolons) here. In the Go language rule, it specifies that a semicolon is inserted after '} ' if this is the final tag of the line. Therefore, the '} ' after the IF statement will automatically insert a semicolon. In fact our program has become "goif num%2 = = 0 {fmt. Println ("The number is even")}; Semicolon inserted by Goelse {fmt. Println ("The number is odd")} ' "' semicolon inserted after. You can see from the code snippet above that the third line has a semicolon inserted. Because ' if{...} else {...} ' is a separate statement, it should not appear in the middle of a semicolon. Therefore, you need to place the ' else ' statement on the same line after '} '. I have rewritten the program and moved the else statement to the end of the IF statement after '} ' to prevent automatic insertion of semicolons. "' Gopackage mainimport (" FMT ") func main () {num: = if num%2 = = 0 {//checks If number is even fmt. Println ("The number is even")} else {fmt. Println ("The number is odd")}} "[Running Program Online] (Https://play.golang.org/p/hv_27vbIBC) Now the compiler will be happy, and so are we? This chapter of the tutorial is over, thank you for your reading and welcome any comments and feedback. * * Previous Tutorial-[Package (Packages)] (https://studygolang.com/articles/11893) * * * * Next tutorial-[loop] (https://studygolang.com/articles/11924) * *
via:https://golangbot.com/if-statement/
Author: Nick Coghlan Translator: Dingo1991 proofreading: Rxcai
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
2,728 reads ∙1 likes