当前位置:网站首页>Go language - loop statement
Go language - loop statement
2022-07-03 07:40:00 【m0_ fifty-two million three hundred and thirty-nine thousand fi】
Go Circular statements of language
Go There is only one kind of circular statement in language , That's it for
for( sentence 1; sentence 2; sentence 3){ /* ......*/ }
sentence 1 Assign an expression to , It is generally used to set the initial value of the cycle control variable , sentence 2 For conditional control statements , sentence 3 Assign an expression to , It is generally used to set the loop control variable .
Examples of use :
for i:=1;i<=10;i++{
println(i)
}
// Print 1~10
As shown above ,for The use of statements is basically the same as C/C++ Agreement . For the above for In the loop sentence 1 and sentence 3 It can be omitted . sentence 1 And statement 3 Mainly through the set cycle control variables to control the number of cycles , We can also use for Variables outside the statement block to control the loop , I.e. omitted sentence 1 and sentence 3, Only conditional control statements are left ( sentence 2), At this time for The function of the statement is the same as C/C++ in while The functions of the statements are consistent .
for Conditions { /* ..... */ }
Examples of use :
i := 1
for i <= 10{
println(i)
i++
}
// Print 1~10
such for Circular writing format , Can replace while loop .
- Infinite loop
for true{
println("Hello go")
}
// It can also be written as follows , I.e. omitted true
for {
println("Hello go")
}
for-each
By using for-each To realize the function of traversing an iteratable object . Be similar to python Medium for ... in....
Examples of use :
var arr [3]string = [3]string{
"go", "C", "python"}
for _, value:= range arr{
fmt.Println("Hello ", value)
}
The output is :
Hello go
Hello C
Hello python
explain :range Two values will be returned , One is the index , The other is the value corresponding to the index . When indexes are not available , have access to _ Discard change value .
gossip
because for The loop statement itself can be implemented while The nature of the cycle ,go Language abandoned while sentence , Embodies the go The simplicity of language .
Reference material
- https://www.runoob.com/go/go-for-loop.html
- https://golang.iswbm.com/c01/c01_10.html
边栏推荐
- Usage of requests module
- The difference between typescript let and VaR
- Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
- Project experience sharing: Based on mindspore, the acoustic model is realized by using dfcnn and CTC loss function
- OSI knowledge sorting
- List exercises after class
- [set theory] order relation (partial order relation | partial order set | example of partial order set)
- 密西根大学张阳教授受聘中国上海交通大学客座教授(图)
- Comparison of advantages and disadvantages between most complete SQL and NoSQL
- Lucene hnsw merge optimization
猜你喜欢

Go language foundation ----- 02 ----- basic data types and operators

技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer

【LeetCode】2. Valid Parentheses·有效的括号

技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建

Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward

Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once

Technical dry goods Shengsi mindspire operator parallel + heterogeneous parallel, enabling 32 card training 242 billion parameter model

不出网上线CS的各种姿势

PAT甲级 1031 Hello World for U

Leetcode 213: 打家劫舍 II
随机推荐
Go language foundation ------ 12 ------ JSON
技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
Circuit, packet and message exchange
Go language foundation ----- 01 ----- go language features
研究显示乳腺癌细胞更容易在患者睡觉时进入血液
GoLang之结构体
Es writing fragment process
Collector in ES (percentile / base)
Industrial resilience
The babbage industrial policy forum
Go language foundation ----- 09 ----- exception handling (error, panic, recover)
Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
Industrial resilience
Go language foundation ----- 03 ----- process control, function, value transfer, reference transfer, defer function
Logging log configuration of vertx
Chapter VI - Containers
Go language foundation ----- 15 ----- reflection
Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire
Introduction of transformation flow
论文学习——鄱阳湖星子站水位时间序列相似度研究