当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
MultiTimer v2 重构版本 | 一款可无限扩展的软件定时器
Rsync + inotify remote synchronization
Machine learning - logistic regression
Crawling fragment of a button style on a website
Mysql database explanation (8)
Upgrade MySQL 5.1 to 5.67
多级缓存使用
Advantages, disadvantages and selection of activation function
What if the server is poisoned? How does the server prevent virus intrusion?
群体智能自主作业智慧农场项目启动及实施方案论证会议
cadence SPB17.4 - Active Class and Subclass
Cookie&Session
字符串排序
Codejock Suite Pro v20.3.0
Explanation of redis database (I)
Cap theorem
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
Mysql database explanation (VII)
mysql乐观锁解决并发冲突
shell脚本中的DATE日期计算