当前位置:网站首页>【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 不为空时,打印错误并返回.
边栏推荐
- 限制字符绕过
- Android安全性优化——APP加固
- 作业:iptables防止nmap扫描以及binlog
- 【深入浅出玩转FPGA学习14----------测试用例设计2】
- Summary of the stock problem of state machine dynamic programming
- How to solve types joiplay simulator does not support this game
- Ukraine's foreign ministry: wu was restored to complete the export of food security
- Dry goods | 4 tips for MySQL performance optimization
- MySQL数据库(基础)
- Error occurred while trying to proxy request项目突然起不来了
猜你喜欢

SWM32系列教程6-Systick和PWM

xss绕过:prompt(1)

47. 【Pointers and Arrays】

Gabor filter study notes

go mode tidy出现报错go warning “all“ matched no packages

mysql主从复制及读写分离脚本-亲测可用

binglog日志追踪:数据备份并备份追踪

joiplay模拟器如何调中文

@requestmapping注解的作用及用法

In-depth understanding of the auto-increment operator from two error-prone written test questions
随机推荐
Bypass of xss
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
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
【萌新解题】删除链表的倒数第 N 个结点
46.<list链表的举列>
IOT跨平台组件设计方案
SWM32系列教程6-Systick和PWM
Android安全性优化——APP加固
Gabor filter study notes
xss的绕过
[In-depth and easy-to-follow FPGA learning 14----------Test case design 2]
web漏洞之需要准备的工作
SereTOD2022 Track2代码剖析-面向半监督和强化学习的任务型对话系统挑战赛
xss靶机训练【实现弹窗即成功】
寄存器(汇编语言)
天空云变化案例
Linux 部署mysql 5.7全程跟踪 完整步骤 django部署
Necessary artifacts - AKShare quants
什么是Promise?Promise的原理是什么?Promise怎么用?
joiplay模拟器rtp如何安装