Akik Look at that coder

Public account: Look at that code farmer

The previous installment introduced the use of lists for Go language learning | Go Theme month

  • Definition of a list
  • How a list is declared
  • Inserts elements into the list
  • Remove elements from the list
  • Traverse the list – Accesses each element of the list

This article will continue to take you into the world of Go.

1. Introduction to this paper

Go language learning process control

2. Definition of process control

Common flow controls in Go are if and for, while switch and GOto are mainly for simplifying code.

There are also loop control statements (break and continue),

  • The former function is to break the loop or jump outswitchJudgment.
  • The latter function is to continueforThe next cycle of.

Here’s how to use them.

3. Use the if statement

In Go language, if keywords can be used for conditional judgment in the following format:

ifExpression {branch1
}else if{branch2
}else{branch3
}
Copy the code

The if statement is an important part of a computer program and is used in almost all languages.

Simply put, the if statement checks the specified condition and performs the appropriate code action when the condition is met.

The details are as follows:

package main

import "fmt"

func main(){
   b: =true
   if b{
      fmt.Println("b is true")}}Copy the code

The output is:

1. Use the else statement

In Go, the else statement follows the braces of other statements, usually the last statement of the current block.

Simply put, if nothing else in the if statement is true, the statement following the else statement is executed.

When we add a branch to the if statement, the code looks like this:

package main

import "fmt"

func main(){
   b: =false
   if b{
      fmt.Println("b is true")}else{
      fmt.Println("b is false")}}Copy the code

The output is:

2. Use the else if statement

In most cases, when multiple expressions need to be evaluated in sequence, else if statements can be used.

Else if statements can be executed when the previous if or else if statements are false.

The specific code is shown in the following case:

package main

import "fmt"

func main(){
   i: =2
   if i==3{
      fmt.Println("i is 3")}else if i==2{
      fmt.Println("i is 2")}}Copy the code

The output is:

3. Special writing of if statement

Another special way to write an if statement is to append an execution statement to the if expression and judge the value of the variable.

The following code example looks like this:

package main

import "fmt"

func main(){
   
   if i:=2; i==3{
      fmt.Println("i is 3")}else if i==2{
      fmt.Println("i is 2")}}Copy the code

The output is:

If I :=2; i==3

  • i:=2To execute a statement
  • i==3Is a true judgment statement

4. Use the for statement

All loops in the Go language can be completed using the for keyword

The basic for loop statement has the following format:

`for`Initial statement; Conditional expression; Closing statement {loop body code}Copy the code

The loop body code keeps executing until the conditional expression returns false and exits the loop automatically

1. A for loop example

The specific code is shown in the following case:

package main

import "fmt"

func main(){

   for i:=0; i<10; i++{ fmt.Println("i is",i)
   }
}
Copy the code

The output is:

2. The for statement containing the range clause

The for statement can also be used to traverse data structures. In Go, you can use the for statement of the range clause to traverse most data structures, such as arrays, slices, strings, maps, and channels, without knowing the length of the data structure.

1. Go through the number group and slice

When Go language uses range statement to iterate over number groups and slices, the format is as follows:

for key,value:=range [] type {value} {traversal statement}Copy the code

Key and value represent the subscript of array, slice and each value corresponding to the subscript respectively

The specific code is shown in the following case:

package main

import "fmt"

func main(){
   number:=[]int{1.2.3.4}
   for i,n:=range number{
      fmt.Printf("The %v element is %v\n",i+1,n)
   }
}
Copy the code

The output is:

2. Iterate over the string

The Go language uses the range statement to iterate over a string in the following format:

for key,value:=range STR {range STR}Copy the code

Key and value represent the index of the string and each character of the string respectively

The specific code is shown in the following case:

package main

import "fmt"

func main(){
   str: ="Hello World! value"
   for key,value:=range str{
      fmt.Printf("%v element is %c\n",key+1,value)
   }
}
Copy the code

The output is:

3. The traverse map

When the Go language traverses through the range statement, the format is as follows:

for key,value:=range map{range map}Copy the code

Key and value represent the map index Key and each character corresponding to the index.

The specific code is shown in the following case:

package main

import "fmt"

func main(){
   m :=map[string]int{
      "Hello":1."World":2,}for key,value:=range m{
      fmt.Printf("%v index corresponds to %v\n",key,value)
   }
}
Copy the code

The output is:

5. Use the Switch statement

The switch statement can be thought of as a batch if statement that makes it easy to judge a large number of values. It not only improves the readability of the code, but also improves a lot of operational performance.

The specific code is shown in the following case:

package main

import "fmt"

func main(){
   a: ="Hello"
   switch a{
   case "Hello":
      fmt.Println(1)

   case "hello":
      fmt.Println(2)

   case "World":
      fmt.Println(3)}}Copy the code

The output is:

1. Add a default case to the Switch statement

In a switch statement, you can use the keyword default to specify code to execute if all other case conditions are not met. The default code is generally at the end of the switch statement, but can be placed anywhere else.

The specific code is shown in the following case:

package main

import "fmt"

func main(){
   a: ="Hello World"
   switch a{
   case "Hello":
      fmt.Println(1)

   case "hello":
      fmt.Println(2)

   case "World":
      fmt.Println(3)

   default:
      fmt.Println(Hello world.)}}Copy the code

The output is:

2. Multiple values in one branch

When multiple cases need to be placed together, the code can be expressed as in the following example:

package main

import "fmt"

func main(){
   a: ="Hello World"
   switch a{
   case "Hello"."Hello World":
      fmt.Println(Hello world.)}}Copy the code

The output is:

3. Branch expressions

Case is not just a constant, you can add an expression like if, but switch is not followed by a judgment variable.

The specific code is shown in the following case:

package main

import "fmt"

func main(){
   input: =10
   switch {
   case input>5 && input<15:
      fmt.Println(Hello world.)}}Copy the code

The output is:

6. Use the defer statement

The defer statement helps the developer execute another function before it returns.

The specific code is shown in the following case:

package main

import "fmt"

func main(){

   defer fmt.Println(Hello world.)
   fmt.Println("Hello World!")}Copy the code

The output is:

This code uses a defer statement,

  • This string of code function is implemented firstHello World!After the output,
  • Just implement where defer isHELLO WORLDStatement output.

7. Use the break statement

Use this statement to end blocks of code that loop for, switch, and so on.

The specific code is shown in the following case:

package main

import "fmt"

func main(){
   node: =0
   for a:=4; a>0; a--{ node++ fmt.Printf("The element traversed in %v is: %v\n",node,a)
      break

   }
   fmt.Printf("Break traversal completed,for loop executed % V times",node)
}
Copy the code

The output is:

8. Use the continue statement

Use this statement to end the current loop and start the next iteration of the loop. Use this statement only in for loops.

Examples of specific codes are as follows:

package main

import "fmt"

func main(){
   node: =0
   for a:=4; a>0; a--{ node++ fmt.Printf("The element traversed in %v is: %v\n",node,a)
      continue

   }
   fmt.Printf("Break traversal completed,for loop executed % V times",node)
}
Copy the code

The output is:

If you find this helpful:

1. Click “like” to support it, so that more people can see this article

2, pay attention to the public number: look at that code farmers, we study together and progress together.