当前位置:网站首页>[introduction to go language] 05 go language branch statement
[introduction to go language] 05 go language branch statement
2022-07-16 06:53:00 【Space time traveler er】
List of articles
05 Go Language branch statement
5.1 if sentence
Single branch if sentence
Grammar format :
if Boolean expression {
/* The Boolean expression is true When the */
}
example :
package main
import "fmt"
func main() {
/* Defining local variables */
var a int = 10
/* Use if Statement to determine the Boolean expression */
if a < 20 {
/* If the condition is true Then execute the following statement */
fmt.Printf("a Less than 20\n" )
}
fmt.Printf("a The value of is : %d\n", a)
}
Double branch if sentence
Grammar format :
if Boolean expression {
/* The Boolean expression is true When the */
} else {
/* The Boolean expression is false When the */
}
example :
package main
import "fmt"
func main() {
/* Definition of local variables */
var a int = 100;
/* Judging Boolean expressions */
if a < 20 {
/* If the condition is true Then execute the following statement */
fmt.Printf("a Less than 20\n" );
} else {
/* If the condition is false Then execute the following statement */
fmt.Printf("a Not less than 20\n" );
}
fmt.Printf("a The value of is : %d\n", a);
}
Multiple branches if sentence
Grammar format :
if Boolean expression 1 {
/* In Boolean expressions 1 by true When the */
} else if Boolean expression 2 {
/* In Boolean expressions 1 by false, Boolean expression 2 by true When the */
} else if Boolean expression 3 {
/* The previous Boolean expressions are false, Boolean expression 3 by true When the */
} else if ... {
// Can appear N Multiple else if
} else {
/* All Boolean expressions above are false When the */
}
example :
package main
import "fmt"
func main() {
num := 9
if num < 0 {
fmt.Println(num, "is negative")
} else if num < 10 {
fmt.Println(num, "has 1 digit")
} else {
fmt.Println(num, "has multiple digits")
}
}
if Statement initialization
if Statements can also have an initializer , The initialization formula is if All branches of the statement are executed before judgment .
Grammar format :
if Initialization ; Boolean expression 1 {
} else if Boolean expression 2 {
} else {
}
example :
package main
import "fmt"
func aaaa() {
fmt.Println("aaaa")
}
func bbbb() int {
fmt.Println("bbbb")
return 20
}
func main() {
num := 5
if aaaa(); num < bbbb() {
fmt.Println("cccc <")
} else if num > 0 {
fmt.Println("cccc >")
} else {
fmt.Println("cccc ==")
}
}
if The initialization expression of the statement can also declare variables , The scope of this variable is only in this if In the logical block of the statement .
package main
import "fmt"
func main() {
if num := 9; num < 0 {
fmt.Println(num, "is negative")
} else if num < 10 {
fmt.Println(num, "has 1 digit")
} else {
fmt.Println(num, "has multiple digits")
}
}
if Statement format considerations
- There is no need to use parentheses to enclose conditions
- Braces for each branch {} There must be , Even if there is only one line of statement
- The left parenthesis must be in if or else On the same line
5.2 switch sentence
switch Statement executes different branches based on different conditions , Its functions are similar if else if … else.
switch Basic grammar
switch [ expression ] {
case expression 1:
Sentence block
case expression 2:
Sentence block
case expression 3:
Sentence block
...
default:
Sentence block
}
switch Statement execution flow : Execute expression first , Get the value , Then from top to bottom and case Compare the expressions of , If equal , Just execute the corresponding case Sentence block , And then quit switch control , If none of them match , execute default.
Example :
package main
import "fmt"
func main() {
var a = 5
switch (a) {
case 0:
fmt.Println(" yes 0")
case 1:
fmt.Println(" yes 1")
case 2:
fmt.Println(" yes 2")
case 3:
fmt.Println(" yes 3")
case 4:
fmt.Println(" yes 4")
case 5:
fmt.Println(" yes 5")
case 6:
fmt.Println(" yes 6")
default:
fmt.Println(" It's other values ")
}
}
switch Statement properties
case The following expressions can be constants 、 Variable expression , Even a function call with a return value :
package main
import "fmt"
func main() {
var a = 5
var b = 4
switch (a) {
case 0:
fmt.Println(" yes 0")
case 1:
fmt.Println(" yes 1")
case 2:
fmt.Println(" yes 2")
case 3:
fmt.Println(" yes 3")
case 4:
fmt.Println(" yes 4")
case b+1:
fmt.Println(" yes b+1")
case 5:
fmt.Println(" yes 5")
case 6:
fmt.Println(" yes 6")
default:
fmt.Println(" It's other values ")
}
}
case The data type of the value of the following expressions , It has to be with switch The expression data type of is consistent :
package main
import "fmt"
func main() {
var a = 5
switch (a) {
case "hello":
fmt.Println(" yes hello") // Compiler error : Different types
case 0:
fmt.Println(" yes 0")
case 1:
fmt.Println(" yes 1")
case 2:
fmt.Println(" yes 2")
case 3:
fmt.Println(" yes 3")
case 4:
fmt.Println(" yes 4")
case 5:
fmt.Println(" yes 5")
case 6:
fmt.Println(" yes 6")
default:
fmt.Println(" It's other values ")
}
}
case The following expression can have multiple :
package main
import "fmt"
func main() {
var a = 5
switch (a) {
case 0, 1:
fmt.Println(" yes 0 or 1")
case 2, 3:
fmt.Println(" yes 2 or 3")
case 4, 5:
fmt.Println(" yes 4 or 5")
case 6:
fmt.Println(" yes 6")
default:
fmt.Println(" It's other values ")
}
}
case There is no need to write break, Because by default, there will be . By default ,case Exit after the statement is executed switch, This and other languages ( such as C Language ) Different :
package main
import "fmt"
func main() {
var a = 5
switch (a) {
case 0, 1:
fmt.Println(" yes 0 or 1")
break // Write not write break Are all the same
case 2, 3:
fmt.Println(" yes 2 or 3")
break
case 4, 5:
fmt.Println(" yes 4 or 5")
break
case 6:
fmt.Println(" yes 6")
default:
fmt.Println(" It's other values ")
}
}
default Statement is not necessary :
package main
import "fmt"
func main() {
var a = 5
switch (a) {
case 0, 1:
fmt.Println(" yes 0 or 1")
case 2, 3:
fmt.Println(" yes 2 or 3")
case 4, 5:
fmt.Println(" yes 4 or 5")
case 6:
fmt.Println(" yes 6")
}
}
default Statements may not be placed at the end , No matter what default Where is the statement placed , Are the last to be tested :
package main
import "fmt"
func main() {
var a = 100
switch (a) {
default:
fmt.Println(" It's other values ")
case 0, 1:
fmt.Println(" yes 0 or 1")
case 2, 3:
fmt.Println(" yes 2 or 3")
case 4, 5:
fmt.Println(" yes 4 or 5")
case 6:
fmt.Println(" yes 6")
}
}
switch The following expression can default , Representation at this time swtich true:
package main
import "fmt"
func main() {
var grade string = "B"
switch {
// switch The following expression can default , Representation at this time switch true
case grade == "A" :
fmt.Printf(" good !\n" )
case grade == "B", grade == "C" :
fmt.Printf(" good \n" )
case grade == "D" :
fmt.Printf(" pass \n" )
case grade == "F":
fmt.Printf(" fail, \n" )
default:
fmt.Printf(" Bad \n" );
}
}
swtich Statement initialization
switch It can be followed by an initialization , for example :
switch grade := 90; {
// Equivalent to switch grade := 90; true
case grade > 90:
fmt.Println(" Good grades ...")
case grade >= 60 && grade <= 90:
fmt.Println(" Excellent achievements ")
default:
fmt.Println(" fail, ")
}
type switch
switch Statements can also be used to judge a interface The type of variable actually stored in the variable .
grammar :
switch x.(type) {
case type1:
Sentence block
case type2:
Sentence block
default: /* Optional */
Sentence block
}
example :
package main
import "fmt"
func main() {
var x interface{
}
// Give to separately x Copy the following different types of values ,switch Statements will enter different branches
// x = 5
// x = float64(5.6)
// x = false
// x = "hello"
switch i := x.(type) {
//x.() The format is type assertion
case nil:
fmt.Printf("x The type is : %T", i)
case int:
fmt.Printf("x The type is : int")
case float64:
fmt.Printf("x The type is : float64")
case func(int) float64:
fmt.Printf("x The type is : func(int)")
case bool, string:
fmt.Printf("x The type is : bool or string")
default:
fmt.Printf(" Other types ")
}
}
5.3 select sentence
Explain again when introducing the channel
边栏推荐
- 将字符串s1中所有出现在字符串s2中的字符删除
- Virtual memory location structure (reserved area, code area, stack area, heap area, literal constant area) and variable modifiers (const, auto, static, register, volatile, extern)
- 四舍五入,向下取整,向上取整的使用
- U-boot 2021.01 version compilation
- 01-kNN
- go语言json解析库jsoniter的使用(替换标准库encoding/json)
- Summary of working methods
- Use MessageBox to realize window confession applet (with source code)
- 如何设置树莓派上网功能
- 整理numpy
猜你喜欢

ROS communication mechanism
![[introduction to go language] 12 go language structure (struct) detailed explanation](/img/44/ca65446c5b75bb0860b62906973f85.png)
[introduction to go language] 12 go language structure (struct) detailed explanation

01机器学习:评估指标

如何使用Keil5中的虛擬示波器進行軟件仿真

SSM整合(借鉴版)

Introduction to common memory

Chapter 4 stm32+ld3320+syn6288+dht11 realize voice acquisition of temperature and humidity values (Part 1)

TypeScript基础配置使用教程(在VScode中自动编译)

Chapter I use of DHT11 temperature and humidity sensor

使用Idea IntelliJ查看字节码文件
随机推荐
Record and analysis of Rac abnormal heartbeat (ipreamsfails)
SSM integration (classic self version)
CodeBlocks shortcut key
ROS command
ROS 通信机制
Redis is the fastest to get started in history (attach redis on ECs)
Friendly zeropi uboot, kernel compilation,
[introduction to go language] 12 go language structure (struct) detailed explanation
[introduction to go language] 08 go language array
01-kNN
pyopencv基础操作指南
Various operations of binary tree (leaf node, parent node, search binary tree, delete node in binary tree, depth of binary tree)
001 空指针和野指针
STM32—TIM3输出PWM信号驱动MG996R舵机(按键控制)
01kNN_Regression
[introduction to go language] 14 go language goroutine and channel details
STM32F103 guider - example game Tetris
3.6 formatting numbers and strings
OpenGL 3D graphics development notes, terrain, lighting, shadows, etc
[Go语言入门] 06 Go语言循环语句