头图

Value type, reference type

1. In Go language, value types and reference types have the following characteristics:
a, value type : basic data types, int, float, bool, string, and arrays and structs
Features: Variables store values directly, memory is usually stack, and the stack will be released after the function is called

b. reference type : pointer, slice, map, chan, interface, etc. are all reference types
Features: Variables store an address, and this address stores the final value. The memory is usually allocated heap and recovered by GC.

  • Strictly speaking, the Go language has no reference types.
  • But we can slice map, chan, function, interface, and slice as reference type, which is easy to understand.
  • The pointer type can also be understood as a reference type.

We mentioned heap and stack above, here is a brief introduction

Heap and stack in memory allocation:

  • stack (operating system): automatically allocated and released by the operating system, storing function parameter values, local variable values, etc. The operation mode is similar to the stack in the data structure.
  • heap (operating system): Generally, it is allocated and released by the programmer. If the programmer does not release, it may be reclaimed by the OS when the program ends. The allocation method is similar to a linked list.

Examples of value type and pointer type parameters:

package main

import "fmt"

func main() {
    name := "无尘"
    modify1(name)
    fmt.Println("name的值为:", name)
    modify2(&name)
    fmt.Println("name的值为:", name)
}

func modify1(name string) { //值类型
    name = "wucs"
} 
func modify2(name *string) { //指针类型
    *name = "wucs"
}
//运行结果:
//name的值为: 无尘
//name的值为: wucs

Reference type

map
Example with map type as a parameter:

package main

import "fmt"

func main() {
    m:=make(map[string]int)
    m["无尘"] = 18
    fmt.Println("无尘的年龄为",m["无尘"])
    modify(m)
    fmt.Println("无尘的年龄为",m["无尘"])
}
func modify(p map[string]int)  {
    p["无尘"] =20
}
//运行结果:
//无尘的年龄为 18
//无尘的年龄为 20
  • We see that the parameter type of the function modify is map, and the data is still modified successfully.
  • In fact, when creating a map, the runtime.makemap function is finally called. The makemap function returns a hmap type, which means it returns a pointer, so the map we created is actually a hmap.
  • Because map is essentially a pointer, the original data can be modified through the parameters of the map type.

    // makemap implements Go map creation for make(map[k]v, hint).
    func makemap(t *maptype, hint int, h *hmap) *hmap{
    //省略无关代码
    }

chan
Channel is essentially a pointer, look at the source code:

func makechan(t *chantype, size int64) *hchan {
    //省略无关代码
}

You can see that the created chan is actually *hchan, so it is also the same as map in the parameter passing.

Zero value of type

  1. In the Go language, variable definitions can be declared or through make , new functions, the difference is that the make and new functions belong to the display declaration and initialization.
  2. If the variable we declare is not initialized explicitly, then the default value of the variable is the zero value for the type.
Types ofZero value
Numerical type (int, float, etc.)0
boolfalse
string"" (empty string)
structZero value of internal field
slicenil
mapnil
pointernil
functionnil
channil
interfacenil
In the Go language, function only has the value transfer , and the actual parameters passed are a copy of the original data. If the copied content is a value type, then the original data cannot be modified in the function; if the copied content is a pointer (or can be understood as a reference type map, chan, etc.), then the original data can be modified in the function.

微客鸟窝
37 声望3 粉丝