当前位置:网站首页>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
边栏推荐
- Educational codeforces round 127 A-E problem solution
- Go语言切片,范围,集合
- Common types of automated testing framework ▏ automated testing is handed over to software evaluation institutions
- 移动app软件测试工具有哪些?第三方软件测评小编分享
- Temporal model: long-term and short-term memory network (LSTM)
- 软件性能测试报告起着什么作用?第三方测试报告如何收费?
- 编译,连接 -- 笔记
- JVM-第2章-类加载子系统(Class Loader Subsystem)
- Special analysis of China's digital technology in 2022
- What is CNAs certification? What are the software evaluation centers recognized by CNAs?
猜你喜欢
cadence SPB17.4 - Active Class and Subclass
mysql乐观锁解决并发冲突
Basic concepts of website construction and management
一刷314-剑指 Offer 09. 用两个栈实现队列(e)
字节面试 transformer相关问题 整理复盘
Special analysis of China's digital technology in 2022
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
Codejock Suite Pro v20.3.0
Mysql database explanation (10)
MySQL集群模式與應用場景
随机推荐
fatal error: torch/extension. h: No such file or directory
MySQL集群模式與應用場景
fatal error: torch/extension.h: No such file or directory
Connect PHP to MSSQL via PDO ODBC
移动app测试如何进行?
Neodynamic Barcode Professional for WPF V11.0
Mysql database explanation (8)
The El tree implementation only displays a certain level of check boxes and selects radio
Configuration of multi spanning tree MSTP
T2 icloud calendar cannot be synchronized
幂等性的处理
Upgrade MySQL 5.1 to 5.69
字节面试 transformer相关问题 整理复盘
网站建设与管理的基本概念
cadence SPB17. 4 - Active Class and Subclass
Mysql database explanation (10)
What if the server is poisoned? How does the server prevent virus intrusion?
How to test mobile app?
Independent operation smart farm Innovation Forum
电脑怎么重装系统后显示器没有信号了