当前位置:网站首页>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
caseIt has to be a communication - all
channelExpressions 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
caseCan run ,SelectOne of them will be selected randomly and fairly . Others will not be executed .
otherwise :
If there isdefaultClause , Then execute the statement .
withoutdefaultClause ,selectWill block , Until some communication can run ;Go It won't be right againchannelOr 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
边栏推荐
- Connect PHP to MySQL via PDO ODBC
- CVPR 2022 优质论文分享
- Date date calculation in shell script
- 布隆过滤器在亿级流量电商系统的应用
- Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
- Mumu, go all the way
- Single architecture system re architecture
- What if the package cannot be found
- 电脑怎么重装系统后显示器没有信号了
- 删除字符串中出现次数最少的字符
猜你喜欢
随机推荐
CVPR 2022 优质论文分享
控制结构(二)
Cookie&Session
Basic concepts of website construction and management
KNN, kmeans and GMM
MultiTimer v2 重构版本 | 一款可无限扩展的软件定时器
What if the server is poisoned? How does the server prevent virus intrusion?
提取不重复的整数
Crawling fragment of a button style on a website
Codejock Suite Pro v20.3.0
Application of Bloom filter in 100 million flow e-commerce system
电脑怎么重装系统后显示器没有信号了
为啥禁用外键约束
php函数
Connect PHP to MSSQL via PDO ODBC
移动app软件测试工具有哪些?第三方软件测评小编分享
时序模型:长短期记忆网络(LSTM)
Node. JS ODBC connection PostgreSQL
Explanation of redis database (I)
Go并发和通道









