Map
Map is the concept of dictionary in Python. Its format is map[keyType]valueType
Let's look at the following code. The reading and setting of map are similar to slice. It is operated through key, but the index of slice can only be of type 'Int', while map has many types, including int, string and all fully defined = = and= Type of operation. For example, bool, number, string, pointer, channel, and interface types, structures and arrays that only contain the previous types
// Declare a dictionary whose key is a string and the value is int. this declaration needs to be initialized with make before use var numbers map[string]int // Another way to declare map numbers = make(map[string]int) numbers["one"] = 1 //assignment numbers["ten"] = 10 //assignment numbers["three"] = 3 fmt.Println("The third number is: ", numbers["three"]) // Read data // Print out for example: the third number is: 3
Points to note in using map:
- The map is out of order. The printed map will be different every time. It cannot be obtained through index, but must be obtained through key
- The length of a map is not fixed, that is, like slice, it is also a reference type
- The built-in len function is also applicable to the map and returns the number of key s owned by the map
- The value of map can be easily modified. Through numbers["one"]=11, the dictionary value with key one can be easily changed to 11
- Unlike other basic types, map is not thread safe. When accessing multiple go routines, mutex lock mechanism must be used
map is also a reference type. If two maps point to an underlying layer at the same time, one will change and the other will change accordingly:
m := make(map[string]string) m["Hello"] = "Bonjour" m1 := m m1["Hello"] = "Salut" // Now the value of m["hello"] is Salut
make and new operations
make is used for memory allocation of built-in types (map, slice and channel). new is used for various types of memory allocation.
The built-in function new is essentially the same as the function of the same name in other languages: new(T) allocates the memory space of type T filled with zero value and returns its address, that is, a value of type * t. In Go terms, it returns a pointer to the zero value of the newly allocated type T. One thing is very important:
new returns a pointer.
The built-in function make(T, args) has different functions from new(T). Make can only create slice, map and channel, and return a T type with initial value (non-zero), instead of * t. Essentially, the reason why these three types are different is that references to data structures must be initialized before use. For example, a slice is a three term descriptor that contains a pointer to data (internal array), length and capacity; Slice is nil before these items are initialized. For slice, map and channel, make initializes the internal data structure and fills in appropriate values.
make returns the initialized (non-zero) value.
Zero value (default)
For "zero value", it does not mean a null value, but a default value of "before variable is filled", which is usually 0. Zero values for some types are listed here
int 0 int8 0 int32 0 int64 0 uint 0x0 rune 0 //The actual type of run is int32 byte 0x0 // The actual type of byte is uint8 float32 0 //4 byte s in length float64 0 //The length is 8 byte s bool false string ""
Processes and functions
if
The if conditional judgment statement in Go does not need parentheses, as shown in the following code
if x > 10 { fmt.Println("x is greater than 10") } else { fmt.Println("x is less than 10") }
Another powerful feature of Go's if is that a variable is allowed to be declared in the conditional judgment statement. The scope of this variable can only be within the conditional logic block, and other places will not work, as shown below
// Calculate and obtain the value x, and then judge whether it is greater than 10 according to the size returned by X. if x := computedValue(); x > 10 { fmt.Println("x is greater than 10") } else { fmt.Println("x is less than 10") } //If this place is called in this way, there will be a compilation error, because x is a variable in the condition fmt.Println(x)
Multiple conditions are as follows:
if integer == 3 { fmt.Println("The integer is equal to 3") } else if integer < 3 { fmt.Println("The integer is less than 3") } else { fmt.Println("The integer is greater than 3") }
for
There is no while in go language. You can use for to realize the function of while
Syntax of the for loop:
for expression1; expression2; expression3 { //... }
for loop example:
package main import "fmt" func main(){ sum := 0; for index:=0; index < 10 ; index++ { sum += index } fmt.Println("sum is equal to ", sum) } // Output: sum is equal to 45
Sometimes if we ignore expression1 and expression3:
sum := 1 for ; sum < 1000; { sum += sum }
Among them; It can also be omitted, so it becomes the following code. Is it deja vu? Yes, this is the function of while.
sum := 1 for sum < 1000 { sum += sum }
There are two key operations in the loop: break and continue. Break is to jump out of the current loop and continue is to skip this loop. When the nesting is too deep, break can be used with the tag, that is, jump to the position specified by the tag. For details, refer to the following examples:
for index := 10; index>0; index-- { if index == 5{ break // Or continue } fmt.Println(index) } // break print out 10, 9, 8, 7, 6 // continue print out 10, 9, 8, 7, 6, 4, 3, 2, 1
break and continue can also be followed by labels to jump to the outer loop in multiple loops
for range can be used to read slice and map data:
for k,v:=range map { fmt.Println("map's key:",k) fmt.Println("map's val:",v) }
Because Go supports "multi value return", the compiler will report an error for variables that are "declared but not called". In this case, you can use_ To discard unwanted return values, for example:
for _, v := range map{ fmt.Println("map's val:", v) }
swich
i := 10 switch i { case 1: fmt.Println("i is equal to 1") case 2, 3, 4: fmt.Println("i is equal to 2, 3 or 4") case 10: fmt.Println("i is equal to 10") default: fmt.Println("All I know is that i is an integer") }