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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.93.20