1. Pointer
Different fromC/C++
pointer inGo
Pointers in languages cannot be offset and operated, and are safe pointers.
To understandGo
Pointers in languages need to know three concepts first: pointer address, pointer type and pointer value.
Go
Function parameters in languages are all value copies. When we want to modify a variable, we can create a pointer variable to the address of the variable.
Passing data uses pointers without copying the data. Type pointers cannot be offset and operation.
Go
Pointer operation in language is very simple, you only need to remember two symbols: & (take the address) and * (take the value according to the address).
1.1 Pointer address and pointer type
Each variable has an address at runtime, which represents the location of the variable in memory.Go
Used in language&
Put characters in front of variables and perform "address fetching" operations on the variables.
Go
Value types in language (int
、float
、bool
、string
、array
、struct
) All have corresponding pointer types, such as:*int
、*int64
、*string
wait.
The syntax for taking variable pointers is as follows:
ptr := &v // The type of v is T
in:
-
v
: The variable representing the address being fetched, of typeT
-
ptr
: variable used to receive address,ptr
The type is*T
, calledT
pointer type.*
Represents pointer.
package main import "fmt" func main() { a := 10 b := &a ("a:%d ptr:%p\n", a, &a) // a:10 ptr:0xc00001a078 ("b:%p type:%T\n", b, b) // b:0xc00001a078 type:*int (&b) // 0xc00000e018 }
1.2 Pointer value
Used on normal variables&
After the operator takes the address, it will get a pointer to this variable, and then it can be used for the pointer.*
Operation, that is, pointer value.
package main import "fmt" func main() { //Pointer value a := 10 b := &a // Take the address of variable a and save the pointer to b ("type of b: %T\n", b) c := *b // Pointer value (memory value according to pointer) ("type of c: %T\n", c) ("value of c: %v\n", c) }
Output result:
type of b: *int
type of c: int
value of c: 10
Address operator&
and value operators*
is a pair of complementary operators.&
Take out the address,*
Extract the value pointed to by the address according to the address.
The relationship and characteristics of variables, pointer addresses, pointer variables, address and value are as follows:
- Fetch the address of the variable (
&
) operation to obtain the pointer variable of this variable. - The value of the pointer variable is the pointer address.
- Take value of pointer variables (
*
) operation to obtain the value of the original variable pointed to by the pointer variable.
package main import "fmt" func p1(n int) { n = 100 } func p2(n *int) { *n = 100 } func main() { a := 10 p1(a) (a) // 10 p2(&a) (a) // 100 }
1.3 Null pointer
- When a pointer is defined and is not assigned to any variable, its value is
nil
- Judgment of a null pointer
package main import "fmt" func main() { var p *string ("The value of p is %v \n", p) if p != nil { ("Non-null pointer") } else { ("Null pointer") } }
1.4 Use of new
new
It is a built-in function, and its function signature is as follows:
func new(Type) *Type
in:
-
Type
Represents the type,new
The function only accepts one parameter, which is a type -
*Type
Represents a type pointer,new
The function returns a pointer to the memory address of that type.
new
Functions are not very commonly used, usenew
The function gets a pointer of type, and the pointer is a zero value of that type for the corresponding value.
func main() { a := new(int) b := new(bool) ("%T\n", a) // *int ("%T\n", b) // *bool (*a) // 0 (*b) // false }
var a *int
Just declared a pointer variablea
However, it is not initialized. The pointer as a reference type needs to be initialized before it has memory space before it can be assigned a value. The built-in should be used as followsnew
Function paira
After initialization, you can assign values to it normally:
func main() { var a *int a = new(int) *a = 10 (*a) }
make
It is also used for memory allocation, which is different fromnew
, it is only used forslice
、map
as well aschan
memory creation, and the type it returns is these three types themselves, not their pointer type, because these three types are reference types, so there is no need to return their pointer.
1.5 The difference between new and make
- Both are used for memory allocation.
-
make
Only forslice
、map
as well aschannel
The initialization of the three reference types are returned; - and
new
Used for memory allocation of types, and the corresponding value of memory is type zero value, returning a pointer to the type.
2. Map
map
It is an unordered key-value-based data structure. Maps in Go language are reference types and must be initialized to be used.
2.1 What is Map
key, value storage
Most commonly said:Map
It's a kind of passingkey
Come and get itvalue
A data structure whose underlying storage method is an array, when storedkey
Can't be repeated whenkey
When repeating,value
Contains coverage, we passkey
conducthash
Operation (can be simply understood askey
Convert to a shaping number) and then take the remainder of the length of the array to obtainkey
In which subscript position of the array is stored, thekey
andvalue
Assemble into a structure and put it in the subscript of the array.
hash conflict
Only one element can be stored in an array subscript, which means that only one pair of array subscripts can be stored in an array.key
,value
, hashkey(xiaoming)=4
Occupies the position of subscript 0, assuming we encounter another onekey
,hashkey(xiaowang)
Too4
, this ishash
Conflict (differentkey
go throughhash
The value obtained afterwards is the same), thenkey=xiaowang
How to store it?
Common solutions to hash conflicts
-
Open addressing method:That is to say, when we store one
key
,value
When foundhashkey(key)
The subscript has been denotedkey
Occupancy, then we find another unoccupied storage of this conflict in the space in this array.key
, then there are many that are not occupied, which one should I find? Common ones include: linear detection method, linear compensation detection method, and random detection method, which is compared with linear detection. -
Zipper method:What is a zipper? It is simply understood as a linked list.
key
ofhash
When conflict, we form a linked list on the elements at the conflicting location, and connect them through pointers. When searching, we find thatkey
For conflict, follow the link list and search until the end node of the link list. If it cannot be found, it will return empty.
Advantages and disadvantages of open addressing (linear detection) and zippers
- Zipper method is simpler than linear detection
- Linear detection and searching will be zippered and will consume more time
- Linear detection will be more likely to cause capacity expansion, while zippers will not
- The zipper stores pointers, so the space will take up a little more than linear detection.
- Zippers apply for storage space dynamically, so they are more suitable for uncertain chain length.
2.2 Map definition
Go
In languageMap
The definition syntax is as follows:
map[KeyType]ValueType
in:
-
KeyType
: Indicates the type of the key. -
ValueType
: Indicates the type of value corresponding to the key.
map
The default initial value of a variable of type isnil
, need to usemake()
Functions to allocate memory. The syntax is:
make(map[KeyType]ValueType, [cap])
incap
expressmap
Although this parameter is not necessary, we should initialize itmap
When it is time to assign a suitable capacity.
2.3 Basic use of map
map
The data in it appears in pairs.map
The basic use is as follows:
func main() { scoreMap := make(map[string]int, 8) scoreMap["Zhang San"] = 90 scoreMap["Li Si"] = 100 (scoreMap) (scoreMap["Li Si"]) ("type of a: %T\n", scoreMap) }
Output result:
map[Li Si:100 Zhang San:90]
100
type of a: map[string]int
map
It is also supported to fill elements when declaring:
func main() { userInfo := map[string]string{ "username": "admin", "password": "123456", } (userInfo) }
2.4 Map traversal
Go
Used in languagefor range
Traversalmap
:
func main() { scoreMap := make(map[string]int) scoreMap["Zhang San"] = 90 scoreMap["Li Si"] = 100 scoreMap["Wang Wu"] = 60 for k, v := range scoreMap { (k, v) } }
If you just want to go throughkey
When you are, you can write it as follows:
func main() { scoreMap := make(map[string]int) scoreMap["Zhang San"] = 90 scoreMap["Li Si"] = 100 scoreMap["Wang Wu"] = 60 for k := range scoreMap { (k) } }
Notice:Traversalmap
The order of elements when the key-value pairs are added has nothing to do with the order in which key-value pairs are added.
2.5 map determines whether a key exists
Go
There is a judgment in the languagemap
The special writing method of whether the middle key exists, the format is as follows:
value, ok := map[key]
ifkey
existok
fortrue
,value
is the corresponding value; does not existok
forfalse
,value
Zero value of value type
func main() { scoreMap := make(map[string]int) scoreMap["Zhang San"] = 90 scoreMap["Li Si"] = 100 // If the key exists that ok is true, value is the corresponding value; if ok is false, value is the zero value of the value type value, ok := scoreMap["Zhang San"] if ok { (v) } else { ("No such person") } }
2.6 map uses delete() function to delete key-value pairs
usedelete()
Built-in functions frommap
Delete a set of key-value pairs indelete()
The format of the function is as follows:
delete(map, key)
in:
-
map
: Indicates that you want to delete the key-value pairmap
-
key
: The key indicating the key-value pair to be deleted
func main(){ scoreMap := make(map[string]int) scoreMap["Zhang San"] = 90 scoreMap["Li Si"] = 100 scoreMap["Wang Wu"] = 60 delete(scoreMap, "Li Si")//Delete Li Si: 100 from map for k,v := range scoreMap{ (k, v) } }
The above is the detailed content of the sample tutorial on using Go Quick Start Pointer Map. For more information about the Go Language Start Pointer Map tutorial, please follow my other related articles!