1.map
Map is of reference type and defaults to nil if the declaration has no initialization value. An empty slice can be used directly because it has an underlying array. An empty map cannot be used directly. You need to make it before using it.
//1, declare map default value nil
var m1 map[key_data_type]value_data_type Specifies the variable namemap[Data type of key] Data type of value//2, use the make declaration
m2:=make(map[key_data_type]value_data_type)
//3, directly declare and initialize the assignment map method
m3:=map[string]int{"Chinese":89."Mathematics":23."English":90}
Copy the code
1.1 the map using
- Insert and update syntax :map[key]=value
- Delete (map, key) delete(map, key)
- Access syntax map[key]
/ / traverse map
for key, val := range map1 {
fmt.Println(key, val)
}
Copy the code
1.2 Underlying Principles of MAP
Source code parsing
struct Hmap
{
uint8 B; // Can hold 2^B entries
uint16 bucketsize; // The size of each bucket
byte *buckets; // 2^B Buckets
byte *oldbuckets; // Buckets is not empty only when capacity is being expanded
};
struct Bucket
{
uint8 tophash[BUCKETSIZE]; // The higher 8 bits of the hash value.... The low level is located from the array of the bucket to the bucket
Bucket *overflow; // Overflow bucket list, if any
byte data[1]; // BUCKETSIZE keys followed by BUCKETSIZE values
};
// BUCKETSIZE is macros defined as 8. Each bucket holds up to 8 key/value pairs. If more than 8 key/value pairs are stored, a new bucket is requested and overflow points to it
Copy the code
- The keys and values of buckets are placed together and values are placed together.
- Capacity expansion uses incremental capacity expansion: a new table is created that is twice the size of the original table, and when the oldbucket is moved to the new table, the oldbucket is not removed from the oldbucket, but marked as deleted. When the hash table is expanded, the old pairs need to be hashed back into the new table. This is done step by step (move 1 or 2 pairs at a time during insert and remove).
Lookup process
- Calculates the hash value based on the key.
- If an old table exists, first search from the old table, and if the bucket found has been evacuated, go to step 3. Otherwise, the corresponding value is returned.
- Find the corresponding value in the new table.
Insertion process analysis
- Calculate the hash value based on the key, and then obtain the corresponding bucket.
- If the bucket is in the old table, re-hash it into the new table.
- In the bucket, find the free location and update the value of the key to be inserted if it already exists.
- Determine whether to grow a table based on the number of elements in the table.
- If the corresponding bucket is full, apply for a new bucket as an overbucket.
Insert the key/ Value pair into the bucket.
2. Process control
- The normal execution of our program is top to bottom line execution, called sequential structure.
- The structures in a program that are executed only when certain conditions are met are called selection structures (if, switch).
- Code that executes over and over again when a condition is met is called a loop structure (for)
2.1 if statement
ifBoolean expression {// Execute code when Boolean condition is true
}
ifBoolean expression {// Condition set to execute code
}else{
// The condition is not valid
}
ifBoolean expression {// Execute code when Boolean condition is true
}else ifBoolean expression2{
// Execute code when Boolean condition is flase
}else{
// None of the above is valid to execute the code
}
Copy the code
Special notation: In Go language, an execution statement can be added after if and between the condition judgment, and the execution result can be used for the subsequent condition judgment. (Execute statement, then judge condition)
if a :=1; a==1{}Copy the code
2.2 Switch Branch Statements
If conditions can only be bool, but switch conditions can be other types. Case values must be unique because only one case block can be entered.
Break and fallThrough break directly jump out of the entire switch statement block fallthrough the current case is executed, continue to execute the following case
2.3 For loop statement
Grammar:forinit; condition; Post {} init Initializes the condition only onceboolType execution condition Continue if the loop body follows if not {} Loop body the POST expression is executed after the loop body completes executionCopy the code
Break: Jumps out of the current loop. If the break is followed by a label, jump to the label
Continue: Terminates the current loop and enters the next loop
Goto: Used the same way as break
3. Value and reference passing
Variables in Golang can be divided into the following two types
- Value types: int, float, string, bool, array, struct
- Reference types: Slice, Pointer, map, chan, etc
Value types pass the value itself, and modification does not affect the original value. Reference types pass memory addresses and can be modified to affect the original value.
4. Deep copy and shallow copy
A shallow copy copies the memory address and operates on the same object, but a deep copy creates a new object and copies the contents of the source object. Use the deep-copy data function: copy(target slice, data source)