当前位置:网站首页>【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 不为空时,打印错误并返回.
边栏推荐
- 如何在WordPress网站上添加导航菜单
- Installation considerations for pytorch
- 正则表达式密码策略与正则回溯机制绕过
- Gabor滤波器学习笔记
- .NET Cross-Platform Application Development Hands-on Tutorial | Build a Kanban-style Todo App with Uno Platform
- 47.【指针与数组】
- Learn Scope from a Compilation Perspective!
- ES 中时间日期类型 “yyyy-MM-dd HHmmss” 的完全避坑指南
- 对象集合去重的方法
- The first level must project independently
猜你喜欢

The difference between h264 and h265 decoding

从两个易错的笔试题深入理解自增运算符

【c语言课程设计】C语言校园卡管理系统

jira是什么

encrypted transmission process

Oracle一个诡异的临时表空间不足的问题

.NET Cross-Platform Application Development Hands-on Tutorial | Build a Kanban-style Todo App with Uno Platform

Encapsulate and obtain system user information, roles and permission control

pytorch bilinear interpolation

mysql主从复制及读写分离脚本-亲测可用
随机推荐
MySQL中substring与substr区别
The first level must project independently
Neural Network (ANN)
MySql数据恢复方法个人总结
binglog日志追踪:数据备份并备份追踪
jira是什么
firewalld
Jetpack Compose学习(8)——State及remeber
transition过渡&&animation动画
【多线程】
Necessary artifacts - AKShare quants
DNS resolution process [visit website]
How to Repair Word File Corruption
What are the efficient open source artifacts of VSCode
How to solve the error of joiplay simulator
Summary of the stock problem of state machine dynamic programming
Error ER_NOT_SUPPORTED_AUTH_MODE Client does not support authentication protocol requested by serv
Gabor filter study notes
Ukraine's foreign ministry: wu was restored to complete the export of food security
How to install joiplay emulator rtp