-
go基础教程之Go if/else语句实例
本站最新发布 go从入门到精通|go基础教程
试听地址 https://www.xin3721.com/eschool/go.html
试听地址 https://www.xin3721.com/eschool/go.html
在Go语言中的分支,if
和else
是直接的。
所有的示例代码,都放在
F:\worksp\golang
目录下。安装Go编程环境请参考:http://www.yiibai.com/go/go_environment.html
这里有一个基本的例子。
一个if
语句可以不用else
语句。
语句可以在条件语句之前; 在此语句中声明的任何变量在所有分支中都可用。
注意,Go语言中的条件不需要围绕括号,但是大括号是必需的。
在Go
语言中没有三元组,所以即使对于基本条件,也需要使用一个完整的if
语句。
if-else.go
的完整代码如下所示 -
package main
import "fmt"
func main() {
// Here's a basic example.
if 7%2 == 0 {
fmt.Println("7 is even")
} else {
fmt.Println("7 is odd")
}
// You can have an `if` statement without an else.
if 8%4 == 0 {
fmt.Println("8 is divisible by 4")
}
// A statement can precede conditionals; any variables
// declared in this statement are available in all
// branches.
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")
}
}
// Note that you don't need parentheses around conditions
// in Go, but that the braces are required.
执行上面代码,将得到以下输出结果 -
F:\worksp\golang>go run if-else.go
7 is odd
8 is divisible by 4
9 has 1 digit
//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/go/golang-if-else.html
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能