Hi, I’m Mingo.

In their learning Golang this time, I wrote a detailed study notes on my personal number WeChat public Go programming time, for the language, I was a beginner, so writing should fit in with the new to classmates, if you are just learning the language, don’t focus on prevention, study together, Grow together.

My Github:github.com/iswbm/GolangCodingTime my online blog: golang.iswbm.com


The process control methods in Go are still quite rich, and there are as follows:

  • If-else condition statement
  • Switch-case select statement
  • For-range loop statement
  • Goto Unconditional jump statement
  • Defer execution

Last time we talked about switch-case, today we’ll talk about for loops.

0. Statement model

This is the basic model of the for loop.

for [condition |  ( init; condition; increment ) | Range]
{
   statement(s);
}Copy the code

As you can see, there are three types of expressions that follow for.

  1. Let’s do a conditional expression
  2. And then three expressions
  3. And then we have a range expression

But there’s actually a fourth

  1. Disjointed expression

1. Follow a conditional expression

This example prints a number from 1 to 5.

a := 1
for a <= 5 {
    fmt.Println(a)
    a ++ 
}Copy the code

The output is as follows

One, two, three, four, fiveCopy the code

2. Follow three expressions

For, followed by three expressions, uses; Space.

Each of these three expressions has its own purpose

  • The first expression initializes a control variable that is run only once during the entire lifecycle of the loop.
  • Second expression: set loop control condition, return true, continue loop, return false, end loop;
  • Third expression: increments or decays the control variable at the beginning of each cycle (except the first).

The example over here is equivalent to the example up here.

import "fmt"

func main() {
    for i := 1; i <= 5; i++ {
        fmt.Println(i)
    }
}Copy the code

The output is as follows

One, two, three, four, fiveCopy the code

2. No expression: infinite loop

In Go, there is no while loop, and if you want to do an infinite loop, you can do it for.

When you do not add any criteria, this means that every time you say true, the program will always be running. However, we do not want to keep the program in an infinite loop. If certain criteria are met, we can use the break keyword to exit the loop, or we can use the continue keyword to skip to the next loop.

Both of the following ways are infinite loops.

For {block} // equivalent to for; {code block}Copy the code

For example

import "fmt"

func main() {
    var i int = 1
    for {
        if i > 5 {
            break
        }
        fmt.Printf("hello, %d\n", i)
        i++
    }
}Copy the code

The output is as follows

hello, 1
hello, 2
hello, 3
hello, 4
hello, 5Copy the code

3. The for-range statement is added

Iterating over an iterable is a very common operation. Go can be implemented in a for-range manner.

Range can connect to array, slice, string, etc

Since range returns two values: index and data, use _ if your code doesn’t use index.

import "fmt" func main() { myarr := [...] string{"world", "python", "go"} for _, item := range myarr { fmt.Printf("hello, %s\n", item) } }Copy the code

The output is as follows

hello, world
hello, python
hello, goCopy the code

A series of reading

01. Setup of development environment (Goland & VS Code)

02. Learn five ways to create variables

03. Data type: **Integer and floating point **

Data types: byte, RUNe, and String

05. Data types: Array and slice

06. Data types: dictionary and Boolean

07. Data type: pointer

08. Object-oriented Programming: Structures and Inheritance

09. An article to understand the function in Go

10. Go language flow control: if-else conditional statements

11. Go language flow control: switch-case selection statement

12. Go language flow control: for loop statement

13. Go language flow control: GOto unconditional jump

14. Go language flow control: defer the call

15. Object-oriented programming: Interfaces and polymorphism

Key words: make and new difference?

17. An article to understand the blocks and scopes in Go

18. Learn Go coroutine: Goroutine

19. Learn Go coroutines: Detail channels/channels

20. Several classic error cases of channel deadlock

21. Learn the Go coroutine: WaitGroup

22. Learn Go coroutines: mutex and read-write locks

23. Exception handling in Go: Panic and recover

24. Super detailed interpretation of Go Modules past life and the introduction to use

25. Go language about package import must learn 8 knowledge points

26. How to open source your modules for others to use?

27. What about type assertions in Go?

28. These five points will help you understand the select usage of Go