This is a creation in Article, where the information may have evolved or changed.
Email: hahayacoder@gmail.com
1 common control structures in the go language are: If statements, switch statements, and for loops
2 If statements are similar to the C language, but the condition is not enclosed in parentheses, it must have curly braces.
//if
varvalue int = 2
ifvalue%2 = = 0 {
FMT. Printf ("%d is even \ n", value)
}
//if...else
ifvalue%2 = = 0 {
FMT. Printf ("%d is even \ n", value)
} else{
FMT. Printf ("%d is odd \ n", value)
}
//if Multi-branch
Ifvalue < 0 {
FMT. Printf ("%d is negative \ n", value)
} Elseif value = = 0 {
FMT. Printf ("%d is 0 \ n", value)
} else{
FMT. Printf ("%d is positive \ n", value)
}
2 switch statement does not break, separate multiple case values with commas
/**switch branch statement Switch Statement No break can use a comma case multiple values ***/
varnumber int = 2
Switchnumber {
case1:
FMT. Println ("one")
case2:
FMT. Println ("both")
case3, 4,5:
FMT. Println ("Three,four,Five")
Default:
FMT. PRINTLN ("Invalidvalue")
}
3 For statement is also similar to C language, just to judge the condition does not use parentheses
/*** for loop ***/
for I: = 1; I < ten; i++ {
FMT. Printf ("%d\t", i)
}
4 The Go language does not have a while statement
5 Small examples
Package Mainimport "FMT" Func Main () {/***IF statement if statement has no parentheses but must have curly braces to note the position of the curly braces ***///ifvar value int = 2if value%2 = = 0 {fmt. Printf ("%d is even \ n", value)}//if...elseif value%2 = = 0 {fmt. Printf ("%d is even \ n", value)} else {fmt. Printf ("%d is odd \ n", value)}//if Multi-branch if value < 0 {fmt. Printf ("%d is negative \ n", value)} else if value = = 0 {fmt. Printf ("%d is 0 \ n", value)} else {fmt. Printf ("%d is positive \ n", value)}/*** for loop ***/for i: = 1; I < 10; i++ {fmt. Printf ("%d\t", i)}fmt. Printf ("\ n")/** Switch Branch statement switch statement does not break can use comma case multiple values ***/var number int = 2switch # {case 1:fmt. Println ("one") case 2:fmt. Println ("a") Case 3, 4, 5:fmt. Println ("Three, four, five") default:fmt. PRINTLN ("Invalid value")}/*** the idea that there is no while loop in the Go Language ***/}