Go grammar review 2

Posted by Cyberspace on Sat, 29 Jan 2022 07:58:06 +0100

1.if statement

In go language, there is a special if statement usage.

if statement,condition{

}

example:

package main

import "fmt"

func main() {
	//Here num is a local variable
	if num := 4; num > 0 {
		fmt.Printf("number=%d\n", num)
	}

	//Global variable here
	num1 := 2
	fmt.Println(num1)

}

result:

number=4
2

2. Random number

Calling the rand keyword can generate random numbers. Random numbers are generated by a source, which we are used to calling as seeds. The type is integer. If the number of seeds is the same, the generated random number is the same. If we want the random numbers to be different, we call seed to change the random numbers.

Example 1:

package main

import (
	"fmt"
	"math/rand"
)

func main() {
	num1 := rand.Int()
	fmt.Println(num1)
}

result:

5577006791947779410

Note: because the number of seeds is fixed, the result is the same no matter how many times it is run.

Example 2:

	for i := 0; i < 10; i++ {
		num := rand.Intn(10)  //Intn(n) stands for randomly generating a number of [0,n)
		fmt.Println(num)
	}

result:

7
7
9
1
8
5
0
6
0
4

Instance 3 (set the number of seeds):

rand.Seed(10)
	num := rand.Intn(10)
	fmt.Println(num)

result:

4

Example 4 (changing the number of seeds):

rand.Seed(100)
	num := rand.Intn(10)
	fmt.Println(num)

result:

3

3. Time stamp

So, how do we operate if we want to generate different random numbers. At this point, we can think of time, which changes randomly. Therefore, we can use time to generate different random numbers. Timestamp: the time difference between the specified time and 0:0:0:0 on January 1, 1970. There are seconds and nanoseconds, which are accessed by Unix and Unix nano respectively.

Example 1:

package main

import (
	"fmt"
	"time"
)

func main() {
	t := time.Now() //Access current time
	fmt.Println(t)
	fmt.Printf("%T", t)
}

result:

2022-01-25 11:48:23.2344592 +0800 CST m=+0.003721801
time.Time (type)


Example 2:

package main

import (
	"fmt"
	"time"
)

func main() {
	t := time.Now() //Access current time
	fmt.Println(t)
	fmt.Printf("%T\n", t)
	t1 := t.Unix()
	fmt.Println(t1)
	t2 := t.UnixNano()
	fmt.Println(t2)
}

result:

2022-01-25 13:14:41.4386937 +0800 CST m=+0.003747301
time.Time          
1643087681         
1643087681438693700

Example 3:

package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	//Set the number of seeds
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < 10; i++ {
		fmt.Println(rand.Intn(10))
	}
}

result:

9
2
7
9
9
0
9
2
8
6

Example 4:

If you want to set the random number between [n,m) (n! = 0), the format is as follows:

rand.Seed(time.Now().Unix())
	for i := 0; i < 10; i++ {
		fmt.Println("-->",
			rand.Intn(10)+3) //[3,13)
	}

4. Traverse the array

Example 1:

package main

import (
	"fmt"
)

func main() {
	var arr [5]int
	for i := 0; i < len(arr); i++ {
		arr[i] = 2*i + 1
	}
	fmt.Println(arr)
	for index, number := range arr {
		fmt.Printf("The subscript is:%d,The values are:%d\n", index, number)
	}
}

result:

[1 3 5 7 9]
Subscript: 0, value: 1
The subscript is: 1 and the value is: 3
Subscript: 2, value: 5
The subscript is 3 and the value is 7
The subscript is 4 and the value is 9

Example 2:

	//If you don't want to take a value, you can replace it with an underscore
	for _, v := range arr {
		sum += v
	}
	fmt.Println(sum)
	sum1 := 0
	for index1, _ := range arr {
		sum1 += index1
	}
	fmt.Println(sum1)

result:

25                 
10     

5. Array and slice copy

package main

import "fmt"

func main() {
	arr := [4]int{0, 1, 2, 3}
	arr1 := arr //pass by value
	arr[0] = 100
	fmt.Println(arr, arr1)

	s := []int{0, 1, 2, 3}
	s1 := s
	s[0] = 100
	fmt.Println(s, s1)

}

result:

[100 1 2 3] [0 1 2 3]
[100 1 2 3] [100 1 2 3]

It can be seen that the copy of the array is to create a new address to store the same data, so the change of the original array arr does not lead to the change of arr1.

However, s of the slice is the address of the data obtained first, and the address of s1 is the same as s, so they change together.

As shown in the figure:

6. Combination of map and slice

package main

import "fmt"

func main() {

	//First person
	map1 := make(map[string]string)
	map1["name"] = "Curry"
	map1["number"] = "30"
	map1["team"] = "Warriors"
	//Second person
	map2 := make(map[string]string)
	map2["name"] = "Thompson"
	map2["number"] = "11"
	map2["team"] = "Warriors"
	//Third person
	map3 := make(map[string]string)
	map3["name"] = "Doncic"
	map3["number"] = "77"
	map3["team"] = "Mavericks"
	//Save the map into slice
	s1 := make([]map[string]string, 0, 6)
	s1 = append(s1, map1)
	s1 = append(s1, map2)
	s1 = append(s1, map3)
	//Traversal slice
	for i, imfor := range s1 {
		fmt.Printf("The first%d Personal information is:\n", i+1)
		fmt.Printf("\t full name:%s\n", imfor["name"])
		fmt.Printf("\t number:%s\n", imfor["number"])
		fmt.Printf("\t team:%s\n", imfor["team"])
	}

}

result:

The first person's information is:
Name: Curry
No.: 30
Team: Warriors
The second person's information is:
Name: Thompson
No.: 11
Team: Warriors
The third person's information is:
Name: Doncic
No.: 77
Team: Mavericks

7. Length and capacity

The length of the slice is obviously the number of elements. The specific length is returned according to the number of elements.
The length of the slice is more like a warning value. If the length is equal to the capacity, the capacity will be expanded, such as

des :=make([]int , 3 , 5)
//At this time, the length is 3 and the capacity is 5, but if you use append(),
//The slice length changes to 4, and using append() again, the slice length changes to 5.
//At this time, the length of the slice is the same as the capacity. At this time, the capacity of the slice increases to cap = len*2

That is, set the warning value through a capacity. If it is equal to the warning value, the slice will be expanded automatically

Topics: Go Back-end