当前位置:网站首页>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
边栏推荐
- Compiling OpenSSL
- T2 icloud calendar cannot be synchronized
- 控制结构(一)
- API gateway / API gateway (II) - use of Kong - load balancing
- Hj31 word inversion
- Connect PHP to MSSQL via PDO ODBC
- fatal error: torch/extension.h: No such file or directory
- [backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)
- 推荐搜索 常用评价指标
- 通过 PDO ODBC 将 PHP 连接到 MySQL
猜你喜欢
随机推荐
Error: unable to find remote key "17f718f726"“
Elk installation
Introduction to dynamic programming of leetcode learning plan day3 (198213740)
How to design a good API interface?
tcp_ Diag kernel related implementation 1 call hierarchy
What if the server is poisoned? How does the server prevent virus intrusion?
调度系统使用注意事项
For examination
Connect PHP to MySQL via PDO ODBC
自动化测试框架常见类型▏自动化测试就交给软件测评机构
Mysql database explanation (IX)
Openstack theoretical knowledge
MySQL Basics
Deeply learn the skills of parameter adjustment
字节面试 transformer相关问题 整理复盘
PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
redis-shake 使用中遇到的错误整理
adobe illustrator 菜單中英文對照
网站压测工具Apache-ab,webbench,Apache-Jemeter
Wechat applet customer service access to send and receive messages