当前位置:网站首页>【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-31 00:22:00 【HUAWEI CLOUD】
前言
if条件语句是一个重要的编程语句,它用于告诉程序在某个条件成立的情况下执行某段程序,而在另一种情况下执行另外的语句.
一、IF语句
1.IF语句的基本使用
Go 语言中,if else 条件判断格式如下:
if 表达式1 { 分支1} else if 表达式2 { 分支2} else { 分支3}- 当表达式1的结果为 true 时,会执行分支1的代码.
- 当表达式1的结果为 false 时,会执行表达式2,若结果为 true, 则会执行分支2的代码.
- 当前面的条件都不满足,即都为 false 时,会执行分支3的代码.
相关案例:
package mainimport "fmt"func main() { // 定义一个字符串 str := 10 // 判断字符串的长度是否等于14 if str == 10 { // 若等于14 fmt.Println("愚公等于10岁") } else { // 若不等于14 fmt.Println("愚公不等于10岁") }}
package mainimport "fmt"func main() { var userName string var userPwd string fmt.Println("请输入用户名:") fmt. Scan (&userName) fmt.Println("请输入密码:") fmt.Scan (&userPwd) //2:对用户名和密码进行校验,如果用户名和密码都输入正确,给出"可以登录系统的提示" if userName == "admin" && userPwd == "88888"{ fmt.Println("可以登录系统") }else if userName == "admin" { fmt. Println("密码输入错误") } else if userPwd == "88888" { fmt.Println("用户名错误!!") }else { fmt.Println("则户名和密码都输入错误!!") }}
2.IF语句的特殊写法
if 还有种特殊写法,我们可以在 if 表达式之前添加一个执行语句,再根据变量进行判断,代码如下:
if err := Connect(); err != nil { fmt.Println(err) return}Connect() 是一个获取数据库连接的函数,它带有返回值,err := Connect() 是一个语句,执行 Connect() 函数后,将错误赋值给 err 变量.
err != nil 是 if 的条件判断表达式,当 err 不为空时,打印错误并返回.
边栏推荐
- [Deep learning] Detailed explanation of Transformer model
- DNS resolution process [visit website]
- Steven Giesel 最近发布了一个由5部分内容组成的系列,记录了他首次使用 Uno Platform 构建应用程序的经验。
- go mode tidy出现报错go warning “all“ matched no packages
- Point Cloud Scene Reconstruction with Depth Estimation
- ES 中时间日期类型 “yyyy-MM-dd HHmmss” 的完全避坑指南
- @requestmapping注解的作用及用法
- Mysql体系化之JOIN运算实例分析
- Game mall table establishment
- An easy-to-use interface testing tools - the Postman
猜你喜欢
Go 学习笔记(84)— Go 项目目录结构

Shell编程之条件语句

Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv

Axure Carousel
![DNS resolution process [visit website]](/img/58/ae9464dc714c4fcb958424ac134c99.png)
DNS resolution process [visit website]

45.【list链表的应用】

Shell script if statement

The difference between h264 and h265 decoding

Axure轮播图

How to adjust Chinese in joiplay simulator
随机推荐
How to open the payment channel interface?
Understand from the 11 common examples of judging equality of packaging types in the written test: packaging types, the principle of automatic boxing and unboxing, the timing of boxing and unboxing, a
How to Repair Word File Corruption
How to ensure the consistency of database and cache data?
Shell脚本 if语句
joiplay模拟器不支持此游戏类型怎么解决
作业:iptables防止nmap扫描以及binlog
论文理解:“Designing and training of a dual CNN for image denoising“
DNS resolution process [visit website]
Add text watermark to PHP image
xss靶机训练【实现弹窗即成功】
神经网络(ANN)
【多线程】
MySQL的grant语句
jira是什么
正则表达式密码策略与正则回溯机制绕过
Android安全性优化——APP加固
Jetpack Compose学习(8)——State及remeber
Jmeter参数传递方式(token传递,接口关联等)
unity2D横版游戏教程4-物品收集以及物理材质