当前位置:网站首页>Go语言条件,循环,函数
Go语言条件,循环,函数
2022-04-23 15:34:00 【世界尽头与你】
1.条件
if/else
package main
import "fmt"
func main() {
var a int = 521
if a == 521 {
fmt.Println("我爱你")
} else {
fmt.Println("我不爱你")
}
}
--------------------------
输出:我爱你
switch-case
switch var1 {
case val1:
...
case val2:
...
default:
...
}
select 语句
select
随机执行一个可运行的 case
。如果没有 case
可运行,它将阻塞,直到有 case
可运行。一个默认的子句应该总是可运行的。
select {
case communication clause :
statement(s);
case communication clause :
statement(s);
/* 你可以定义任意数量的 case */
default : /* 可选 */
statement(s);
}
以下描述了 select
语句的语法:
- 每个
case
都必须是一个通信 - 所有
channel
表达式都会被求值 - 所有被发送的表达式都会被求值
- 如果任意某个通信可以进行,它就执行,其他被忽略。
- 如果有多个
case
都可以运行,Select
会随机公平地选出一个执行。其他不会执行。
否则:
如果有default
子句,则执行该语句。
如果没有default
子句,select
将阻塞,直到某个通信可以运行;Go 不会重新对channel
或值进行求值。
2.循环语句
for
循环是一个循环控制结构,可以执行指定次数的循环。
实例1:计算 1 到 10 的数字之和
package main
import "fmt"
func main() {
sum := 0
for i := 0; i <= 10; i++ {
sum += i
}
fmt.Println(sum)
}
------------------------------------
输出:55
实例2:无限循环
package main
import "fmt"
func main() {
sum := 0
for {
sum += 1
}
fmt.Println(sum)
}
要停止无限循环,可以在命令窗口按下ctrl-c
。
实例3:For-each range
循环
package main
import (
"fmt"
)
func main() {
strings := []string{
"imustctf", "wode"}
for i, s := range strings {
fmt.Println(i, s)
}
}
---------------------------------------
输出:
0 imustctf
1 wode
实例4:类似的while
循环,在 sum 小于 10 的时候计算 sum 自相加后的值:
package main
import "fmt"
func main() {
sum := 1
for sum <= 10 {
sum += sum
}
fmt.Println(sum)
}
-----------------------------
输出:16
3.函数
实例1:函数传入两个整型参数 num1 和 num2,并返回这两个参数的最大值
package main
import "fmt"
func max(num1, num2 int) int {
// 声明局部变量
var result int
if num1 > num2 {
result = num1
} else {
result = num2
}
return result
}
func main() {
var a int = 100
var b int = 200
res := max(a, b)
fmt.Println(res)
}
-----------------------------
输出:200
实例2:函数返回多个值
package main
import "fmt"
func swap(x, y string) (string, string) {
return y, x
}
func main() {
a, b := swap("a", "b")
fmt.Println(a, b)
}
--------------------------------
输出:b a
实例3:引用传递指针参数
package main
import "fmt"
func swap(x *int, y *int) {
var temp int
temp = *x /* 保存 x 地址上的值 */
*x = *y /* 将 y 值赋给 x */
*y = temp /* 将 temp 值赋给 y */
}
func main() {
a, b := 100, 200
fmt.Println(a, b)
swap(&a, &b)
fmt.Println(a, b)
}
-----------------------------------
输出:
100 200
200 100
版权声明
本文为[世界尽头与你]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Gherbirthday0916/article/details/124301725
边栏推荐
- Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
- TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2
- Nacos程序连接MySQL8.0+ NullPointerException
- C语言超全学习路线(收藏让你少走弯路)
- 深度学习调参的技巧
- 网站压测工具Apache-ab,webbench,Apache-Jemeter
- 机器学习——逻辑回归
- 群体智能自主作业智慧农场项目启动及实施方案论证会议
- SSH connects to the remote host through the springboard machine
- HJ31 单词倒排
猜你喜欢
What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it
Cookie&Session
[leetcode daily question] install fence
TLS / SSL protocol details (28) differences between TLS 1.0, TLS 1.1 and TLS 1.2
Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
Tun model of flannel principle
今日睡眠质量记录76分
群体智能自主作业智慧农场项目启动及实施方案论证会议
自主作业智慧农场创新论坛
Knn,Kmeans和GMM
随机推荐
Compiling OpenSSL
Openstack theoretical knowledge
PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
移动app软件测试工具有哪些?第三方软件测评小编分享
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
Kubernetes详解(九)——资源配置清单创建Pod实战
Differential privacy (background)
Baidu written test 2022.4.12 + programming topic: simple integer problem
软件性能测试报告起着什么作用?第三方测试报告如何收费?
MultiTimer v2 重构版本 | 一款可无限扩展的软件定时器
什么是CNAS认证?CNAS认可的软件测评中心有哪些?
控制结构(二)
Elk installation
SSH connects to the remote host through the springboard machine
Deeply learn the skills of parameter adjustment
【Leetcode-每日一题】安装栅栏
激活函数的优缺点和选择
Sorting and replying to questions related to transformer
Introduction to dynamic programming of leetcode learning plan day3 (198213740)
Precautions for use of dispatching system