当前位置:网站首页>[Yugong series] July 2022 go teaching course 017 - if of branch structure
[Yugong series] July 2022 go teaching course 017 - if of branch structure
2022-07-26 14:23:00 【Yugong move code】
List of articles
Preface
if Conditional statement is an important programming statement , It is used to tell the program to execute a certain program when a certain condition is true , In another case, another statement is executed .
One 、IF sentence
1.IF Basic use of sentences
Go In language ,if else The format of condition judgment is as follows :
if expression 1 {
Branch 1
} else if expression 2 {
Branch 2
} else {
Branch 3
}
- When the expression 1 As the result of the true when , Will take branches 1 Code for .
- When the expression 1 As the result of the false when , Will execute expression 2, If the result is true, Branches will be taken 2 Code for .
- When the previous conditions are not met , That is to say, all are false when , Will take branches 3 Code for .
Related cases :
package main
import "fmt"
func main() {
// Define a string
str := 10
// Judge whether the length of the string is equal to 14
if str == 10 {
// If it is equal to the 14
fmt.Println(" Yu Gong is equal to 10 year ")
} else {
// If not equal to 14
fmt.Println(" Yu Gong is not equal to 10 year ")
}
}

package main
import "fmt"
func main() {
var userName string
var userPwd string
fmt.Println(" Please enter a user name :")
fmt. Scan (&userName)
fmt.Println(" Please input a password :")
fmt.Scan (&userPwd)
//2: Verify the user name and password , If the user name and password are entered correctly , give " You can log in to the system "
if userName == "admin" && userPwd == "88888"{
fmt.Println(" You can log in to the system ")
}else if userName == "admin" {
fmt. Println(" Wrong password ")
} else if userPwd == "88888" {
fmt.Println(" User name error !!")
}else {
fmt.Println(" Then the account name and password are entered incorrectly !!")
}
}

2.IF Special writing of sentences
if There is also a special way of writing , We can do it in if Add an execution statement before the expression , Then judge according to the variables , The code is as follows :
if err := Connect(); err != nil {
fmt.Println(err)
return
}
Connect() Is a function to obtain database connection , It has a return value ,err := Connect() Is a statement , perform Connect() After the function , Assign errors to err Variable .
err != nil yes if The conditional expression of , When err Isn't empty , Print the error and return .
边栏推荐
- Research on technology subject division method based on patent multi-attribute fusion
- Plato farm is expected to further expand its ecosystem through elephant swap
- 【无标题】
- Leetcode36 effective Sudoku
- [mathematical modeling] summary of common basic models
- Install dexdump on win10 and remove the shell
- 多线程——线程池
- TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程
- Win11运行虚拟机死机了?Win11运行VMware虚拟机崩溃的解决方法
- Disease knowledge discovery based on spo semantic triples
猜你喜欢

Large and small end mode

OA项目之我的会议

基于专利多属性融合的技术主题划分方法研究

~6. ccf 2021-09-1 数组推导

Mysql-03 database operation

When AI encounters life and health, Huawei cloud builds three bridges for them

Learning basic knowledge of Android security

MySQL-03 数据库操作

@A thousand lines of work, ride the cloud together!

全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL
随机推荐
@千行百业,一起乘云而上!
Redis的数据操作
Mysql-03 database operation
C语言_结构体指针变量引入
Annotation and reflection
Instructions for various interfaces of hand-held vibrating wire collector vh03
Multi task text classification model based on tag embedded attention mechanism
Meeting seating and submission for approval of OA project
C语言_结构体指针来访问结构体数组
Iscc2021 lock problem solution
Mysql-04 storage engine and data type
全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL
Seata的部署与微服务集成
One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
Basic syntax of MySQL DDL and DML and DQL
Mlx90640 infrared thermal imager temperature sensor module development notes (6)
Use of URL download resources
Arithmetic operation and logic operation of image in opencv
Integer internal cache
中部“第一城”,长沙“人才引力”从争先到领先