- Hands-On Full Stack Development with Go
- Mina Andrawos
- 116字
- 2021-07-02 12:33:32
Loops
In Go, there is a single keyword that you can use when you want to write a loop—for. There are no other keywords to indicate a loop in Go.
Let's look at the following code. Let's say we want to loop from 1 to 10; here is how this is done:
for i:=1;i<=10;i++{
//do something with i
}
As in other languages, your for statement needs to include the following:
- An initial value (i:=1) in your code—this is optional
- A condition to indicate whether to keep iterating or not (i<=10)
- The value of the next iteration (i++)
What if we have a slice or an array and we want to iterate over it in a loop? Go comes to the rescue with the concept of for .. range. Let's assume we have a slice called myslice and that we want to iterate over it. Here is what the code would look like:
myslice := []string{"one","two","three","four"}
for i,item := range myslice{
//do something with i and item
}
In the preceding piece of code, i represents the index of the current iteration. For example, if we are at the second item of myslice, then the value of i will be equal to 1 (because the index starts at 0). The item variable, on the other hand, represents the value of the slice item at the current iteration. For example, if we are at the third item of the slice, then we are at item value three.
There are cases where we don't care about the index. For this, we can use the following syntax:
for _,item := range myslice{
//do something with item
}
What about if we only care about the index? For that, we can do this:
for i := range myslice{
//do something with item
}
Someone might ask, why would I need only the index and not the items of the slice themselves? The answer is simple—when you obtain the item from the for..range statement, you only obtain a copy of the item, which means that you won't be able to change the original item that lives in the slice should the need arise. However, when you obtain the index, this gives you the power to change the item inside the slice. There are cases where you would need to change the values inside a slice while you are iterating over it. This is when you use the index. Here is a trivial example:
myslice := []string{"one","two","three","four"}
for i := range myslice {
myslice[i] = "other"
}
fmt.Println(myslice)
//output is: other other other other
But what about the while loop? If you come from any programming language other than Go, you must be fully aware of the concept of the while loop. As we mentioned earlier, in Go, all loops make use of the for keyword, so in other words, for is Go's while as well. Here is an example:
for i>5{
//do something
}
As in other programming languages, Go supports the break and continue keywords. The break keyword inside a loop would cause the loop to break, even if it is not done. The continue keyword, on the other hand, will force a loop to jump to the next iteration.
Now, we'll talk about panic, recover, and defer
- C++ Primer習題集(第5版)
- ASP.NET MVC4框架揭秘
- Arduino by Example
- Linux C/C++服務(wù)器開發(fā)實踐
- HTML5+CSS3+JavaScript Web開發(fā)案例教程(在線實訓版)
- SSM輕量級框架應(yīng)用實戰(zhàn)
- Red Hat Enterprise Linux Troubleshooting Guide
- ABAQUS6.14中文版有限元分析與實例詳解
- ArcPy and ArcGIS(Second Edition)
- Practical Responsive Typography
- Eclipse開發(fā)(學習筆記)
- Python大數(shù)據(jù)與機器學習實戰(zhàn)
- 零基礎(chǔ)學西門子PLC編程:入門、提高、應(yīng)用、實例
- 深入淺出Go語言核心編程
- 編程改變生活:用PySide6/PyQt6創(chuàng)建GUI程序(進階篇·微課視頻版)