if 和 else 分支结构在 Go 中当然是直接了当的了。

package main
import "fmt"
func main() {
这里是一个基本的例子。 if 7%2 == 0 {
fmt.Println("7 is even")
} else {
fmt.Println("7 is odd")
}
你可以不要 else 只用 if 语句。 if 8%4 == 0 {
fmt.Println("8 is divisible by 4")
}
在条件语句之前可以有一个语句;任何在这里声明的变量都可以在所有的条件分支中使用。 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")
}
}

Result:

$ go run if-else.go
7 is odd
8 is divisible by 4
9 has 1 digit

坐标: 上一个例子     下一个例子

最新文章

  1. iOS-多线程基础
  2. 【JAVA并发编程实战】8、锁顺序死锁
  3. maven-腾讯SDK(QQ)接口java引入pom配置
  4. 三、python高级特性(切片、迭代、列表生成器、生成器)
  5. Arbitrage HDU
  6. TCP closing a connection
  7. mtp
  8. linux vi 报错 E37: No write since last change (add ! to override)
  9. tkinter第一章
  10. ConstraintLayout知识记录
  11. P1659 [国家集训队]拉拉队排练
  12. MYSQL后更改ROOT的密码后无法启动服务报错1067
  13. Debian Security Advisory(Debian安全报告) DSA-4416-1 wireshark security update
  14. 请求与响应编码及jsp基本原理
  15. 关于promise的几个认知
  16. 【LeetCode】 Valid Sudoku
  17. Python基础5:列表 元祖 字典 集合 Json
  18. Sourcetree报错: 您没有已经配置扩展集成设置的远端
  19. android 图片旋转 移动 放大缩小
  20. Some web Address

热门文章

  1. Codeforces Round #554 (Div. 2) C.Neko does Maths (gcd的运用)
  2. [CSS布局]简单的CSS三列布局
  3. [Luogu1821][USACO07FEB]银牛派对Silver Cow Party
  4. Code Festival 2017 Qual B E Popping Balls
  5. 使用stylelint进行Vue项目样式检查
  6. c++ ofstream使用方法
  7. 常见Serialize技术探秘(ObjectXXStream、XML、JSON、JDBC byte编码、Protobuf)
  8. UITableViewCell的移动
  9. 聊聊 API 签名方式
  10. frida的js脚本处理正则的一个小坑