Go-value types and reference types

Value type : int, float, bool, string, array, structure struct, use these types of variables to directly point to the value in memory, the value type variables are stored in the stack area.

When the equal sign "=" is used to assign the value of one variable to another variable, such as j = i, the value of i is actually copied in memory.

Reference type : refers to three predefined types : slice, map, and channel. Reference types are more copied data structures.

(1) Allocate memory:

(2) Initialize a series of attributes, etc. A reference type variable r1 stores the memory address (number) where the value of r1 is located, or the location of the first byte in the memory address . This memory address is called a pointer .

 

Value type: var num = 100 =>

Reference type: such as ref =>

 

The variable stores an address, and the space corresponding to this address actually stores data.

Published 127 original articles · Likes 24 · Visits 130,000+

Guess you like

Origin blog.csdn.net/Linzhongyilisha/article/details/99630660