Value type, reference type

In the Go language, value types and reference types have the following characteristics: a, value types: primitive data types, int,float,bool,string, array and struct: variables store values directly, memory is usually allocated on the stack, and the stack is freed after a function is called

Reference type: pointer, slice, map, chan, interface, etc. Reference type: A variable stores an address, which stores the final value. Memory is usually allocated on the heap and reclaimed by GC.

  • Strictly speaking, the Go language has no reference types.
  • But we can refer to map, chan, function, interface, slice as reference types for easy understanding.
  • A pointer type can also be thought of as a reference type.

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

The heap and stack in memory allocation:

  • The stack(operating system) : automatically allocated and released by the operating system to store function parameter values, local variable values, etc. It operates like a stack in a data structure.
  • The heap(operating system) : generally allocated by the programmer to release, if the programmer does not release, the end of the program may be recovered by the OS, allocation is similar to the linked list.

Examples of value and pointer type parameters:

Package main import "FMT" func main() {name := "clean" modify1(name) FMT.Println(" Name value is :", Name) modify2(&Name) fmt.println (" Name value is :", } modify1(name string) {// Value type name = "wucs"} Modify2 (name *string) {// Pointers type *name = "wucs"} // Result: // The value of name is: clean // The value of name is: wucs

Reference types

Examples of a map that takes a map type as a parameter:

Package main import "FMT" func main() {m:=make(map[string]int) m[" clean "] = 18 FMT.Println(" clean ",m[" clean "]) modify(m) Fmt.println (" clean age ",m[" clean "])} func modify(p map[string]int) {p[" clean "] =20
  • We can see that function MODIFY has a parameter of type map, and the data is still modified successfully.
  • In fact, when creating the map, the Runtime. MakeMap function is finally called, and the MakeMap function returns oneHMap, which means it returns a pointer, so the map we’re creating is actually a pointerHmap.
  • Because a map is essentially a pointer, the original data can be modified with 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{//

A chan channel is essentially a pointer.

Func makechan(t *chantype, size int64) *hchan {//

You can see that the Chan created is actually *hchan, so it passes parameters just like map.

The zero value of type

  1. In the Go language, variables can be defined byThe statementOr bymake,The new function, the difference is that make and new functions are display declarations and initialized.
  2. If we declare a variable that is not initialized by the declaration shown, then the default value of the variable is zero for the type.
type Zero value
Numeric types (int, float, etc.) 0
bool false
string “”(empty string)
struct The zero value of the internal field
slice nil
map nil
Pointer to the nil
function nil
chan nil
interface nil

In the Go language,
The parameter passing of a function is only value passing, and the arguments passed are copies of the original data. If the content is copied as a value type, the original data cannot be modified in the function; If the contents of the copy are Pointers (or can be understood as reference types Map, Chan, and so on), then the original data can be modified in the function.