当前位置:网站首页>Go language, condition, loop, function
Go language, condition, loop, function
2022-04-23 15:41:00 【The end of the world and you】
Go Language conditions , loop , function
1. Conditions
if/else
package main
import "fmt"
func main() {
var a int = 521
if a == 521 {
fmt.Println(" I love you! ")
} else {
fmt.Println(" I do not love you ")
}
}
--------------------------
Output : I love you!
switch-case
switch var1 {
case val1:
...
case val2:
...
default:
...
}
select sentence
select
Randomly execute a runnable case
. without case
Can run , It will block , Until there is case
Can run . A default clause should always run .
select {
case communication clause :
statement(s);
case communication clause :
statement(s);
/* You can define any number of case */
default : /* Optional */
statement(s);
}
The following describes select
Sentence syntax :
- Every
case
It has to be a communication - all
channel
Expressions are evaluated - All expressions sent are evaluated
- If any particular communication can be made , It will carry out , The rest is ignored .
- If there are more than one
case
Can run ,Select
One of them will be selected randomly and fairly . Others will not be executed .
otherwise :
If there isdefault
Clause , Then execute the statement .
withoutdefault
Clause ,select
Will block , Until some communication can run ;Go It won't be right againchannel
Or value to evaluate .
2. Loop statement
for
Cycle is a cycle control structure , You can execute a specified number of cycles .
example 1: Calculation 1 To 10 The sum of the numbers
package main
import "fmt"
func main() {
sum := 0
for i := 0; i <= 10; i++ {
sum += i
}
fmt.Println(sum)
}
------------------------------------
Output :55
example 2: Infinite loop
package main
import "fmt"
func main() {
sum := 0
for {
sum += 1
}
fmt.Println(sum)
}
To stop the infinite loop , You can press... In the command window ctrl-c
.
example 3:For-each range
loop
package main
import (
"fmt"
)
func main() {
strings := []string{
"imustctf", "wode"}
for i, s := range strings {
fmt.Println(i, s)
}
}
---------------------------------------
Output :
0 imustctf
1 wode
example 4: Allied while
loop , stay sum Less than 10 When the sum The value after self adding :
package main
import "fmt"
func main() {
sum := 1
for sum <= 10 {
sum += sum
}
fmt.Println(sum)
}
-----------------------------
Output :16
3. function
example 1: Function passes in two integer arguments num1 and num2, And return the maximum value of these two parameters
package main
import "fmt"
func max(num1, num2 int) int {
// Declare local variables
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)
}
-----------------------------
Output :200
example 2: Function returns multiple values
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)
}
--------------------------------
Output :b a
example 3: Pass pointer parameter by reference
package main
import "fmt"
func swap(x *int, y *int) {
var temp int
temp = *x /* preservation x The value on the address */
*x = *y /* take y Value is assigned to x */
*y = temp /* take temp Value is assigned to y */
}
func main() {
a, b := 100, 200
fmt.Println(a, b)
swap(&a, &b)
fmt.Println(a, b)
}
-----------------------------------
Output :
100 200
200 100
Copyright notice : This tutorial is based on the rookie tutorial
版权声明
本文为[The end of the world and you]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231533524059.html
边栏推荐
- What if the server is poisoned? How does the server prevent virus intrusion?
- 大型互联网为什么禁止ip直连
- What role does the software performance test report play? How much is the third-party test report charged?
- Treatment of idempotency
- 今日睡眠质量记录76分
- 怎么看基金是不是reits,通过银行购买基金安全吗
- 【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
- Why disable foreign key constraints
- 考试考试自用
- APISIX jwt-auth 插件存在错误响应中泄露信息的风险公告(CVE-2022-29266)
猜你喜欢
G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
Do we media make money now? After reading this article, you will understand
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
激活函数的优缺点和选择
Application of Bloom filter in 100 million flow e-commerce system
使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
Knn,Kmeans和GMM
Functions (Part I)
Codejock Suite Pro v20. three
Cap theorem
随机推荐
网站压测工具Apache-ab,webbench,Apache-Jemeter
Leetcode学习计划之动态规划入门day3(198,213,740)
How to test mobile app?
时序模型:门控循环单元网络(GRU)
Mobile finance (for personal use)
多生成树MSTP的配置
服务器中毒了怎么办?服务器怎么防止病毒入侵?
s16.基于镜像仓库一键安装containerd脚本
一刷313-剑指 Offer 06. 从尾到头打印链表(e)
Independent operation smart farm Innovation Forum
Control structure (I)
深度学习调参的技巧
字节面试 transformer相关问题 整理复盘
What is CNAs certification? What are the software evaluation centers recognized by CNAs?
One brush 313 sword finger offer 06 Print linked list from end to end (E)
Application of Bloom filter in 100 million flow e-commerce system
2022年中国数字科技专题分析
php函数
Deep learning - Super parameter setting
[backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)