SoFunction
Updated on 2025-03-05

Go language quickstart pointer map usage sample tutorial

1. Pointer

Different fromC/C++pointer inGoPointers in languages ​​cannot be offset and operated, and are safe pointers.

To understandGoPointers in languages ​​need to know three concepts first: pointer address, pointer type and pointer value.

GoFunction 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.

GoPointer 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.GoUsed in language&Put characters in front of variables and perform "address fetching" operations on the variables.

GoValue types in language (intfloatboolstringarraystruct) All have corresponding pointer types, such as:*int*int64*stringwait.

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,ptrThe type is*T, calledTpointer 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 isnil
  • 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

newIt is a built-in function, and its function signature is as follows:

func new(Type) *Type

in:

  • TypeRepresents the type,newThe function only accepts one parameter, which is a type
  • *TypeRepresents a type pointer,newThe function returns a pointer to the memory address of that type.

newFunctions are not very commonly used, usenewThe 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 *intJust declared a pointer variableaHowever, 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 followsnewFunction pairaAfter initialization, you can assign values ​​to it normally:

func main() {
    var a *int
    a = new(int)
    *a = 10
    (*a)
}

makeIt is also used for memory allocation, which is different fromnew, it is only used forslicemapas well aschanmemory 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.
  • makeOnly forslicemapas well aschannelThe initialization of the three reference types are returned;
  • andnewUsed for memory allocation of types, and the corresponding value of memory is type zero value, returning a pointer to the type.

2. Map

mapIt 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:MapIt's a kind of passingkeyCome and get itvalueA data structure whose underlying storage method is an array, when storedkeyCan't be repeated whenkeyWhen repeating,valueContains coverage, we passkeyconducthashOperation (can be simply understood askeyConvert to a shaping number) and then take the remainder of the length of the array to obtainkeyIn which subscript position of the array is stored, thekeyandvalueAssemble 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.keyvaluehashkey(xiaoming)=4Occupies the position of subscript 0, assuming we encounter another onekeyhashkey(xiaowang)Too4, this ishashConflict (differentkeygo throughhashThe value obtained afterwards is the same), thenkey=xiaowangHow to store it?

Common solutions to hash conflicts

  • Open addressing method:That is to say, when we store onekeyvalueWhen foundhashkey(key)The subscript has been denotedkeyOccupancy, 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.keyofhashWhen conflict, we form a linked list on the elements at the conflicting location, and connect them through pointers. When searching, we find thatkeyFor 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

GoIn languageMapThe 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.

mapThe default initial value of a variable of type isnil, need to usemake()Functions to allocate memory. The syntax is:

 make(map[KeyType]ValueType, [cap])

incapexpressmapAlthough this parameter is not necessary, we should initialize itmapWhen it is time to assign a suitable capacity.

2.3 Basic use of map

mapThe data in it appears in pairs.mapThe 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

mapIt is also supported to fill elements when declaring:

func main() {
    userInfo := map[string]string{
        "username": "admin",
        "password": "123456",
    }
    (userInfo)
}

2.4 Map traversal

GoUsed in languagefor rangeTraversalmap:

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 throughkeyWhen 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:TraversalmapThe 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

GoThere is a judgment in the languagemapThe special writing method of whether the middle key exists, the format is as follows:

value, ok := map[key]

ifkeyexistokfortrue,valueis the corresponding value; does not existokforfalse,valueZero 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 frommapDelete 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!