This is a creation in Article, where the information may have evolved or changed.
Original link: Understand Go pointers in less than, words or your money back
This is the content of a programmer who is learning the go language and is not familiar with the pointer concept or the pointer type of Go
What is a pointer?
Simply explained, the pointer is the value that points to another piece of address, which is explained in the textbook, but if you are a developer from a language that does not discuss variable address development, programming with pointers will be very nice.
Let's change the subject first.
What is memory?
The memory RAM of the computer can think of it as some orderly box, one by one, and each box or cell is incremented by a unique number, which is the address of the cell, the address of the memory.
Image.png
Each cell stores a single value, and if you know the address of a cell, you can read the contents of the cell through the address. You can also place a value in this cell to replace any previous value.
This is all about memory. Everything the CPU does is read or store the values in the cell.
What is a variable?
Write a program, retrieve the value and store it in a block memory with address 200, multiply it by 3, and store the result in another piece of memory with address 201, and we can write a pseudo-code like this:
- Retrieves a value with a memory address of 200 and stores it in the CPU
- Multiply the value stored in the CPU by 3
Writes the result stored in the CPU to a memory block with address 201
Image.png
This is quite an early form of programming in which programmers need to maintain a list of memory addresses and need to know clearly: who, when, and what values are stored in memory.
Obviously this is a very boring and error-prone way, and it also means that you are assigning addresses to every possible value in memory during the program build process. Even worse, this design makes it hard to dynamically allocate storage for variables, and you can imagine what it would be like to write a large program that only uses global variables.
To solve this problem, the concept of a variable is created, a variable is simply an alphanumeric combination that represents a memory address, or a tag or a nickname.
Now, instead of discussing the memory address, let's say the variable, which is a much easier-to-understand name given to the memory address, which the previous program can now explain:
Image.png
- Gets the value stored in the variable A and stores it in the CPU
- Multiply it by 3
- Save the result in variable B
This is the same program, the only important improvement is that we are not directly concerned about the memory address, we do not need to continuously track the memory address, but instead of this chore to the compiler.
Now we can write the program like this:
var a = 6 var b = a * 3
The compiler will ensure that A and B variables are assigned a unique address, and that their values are not freed until the call ends.
What exactly is a pointer?
As of now we know that memory is a series of storage units with serial numbers, variables are the nicknames that the compiler assigns to memory addresses, so what is a pointer?
The pointer is a value that points to another memory address variable
The pointer points to the memory address of the variable, just like the memory address of the variable's value
Let's take a look at a code snippet
func main() { a := 200 b := &a *b++ fmt.Println(a)}
In the first line of the main function, we define a new variable A and assign a value of 200. Next we define a variable B and assign the address of variable A to B. We do not know the exact storage address of a, but we can still store the address of a in variable B.
Image.png
Image.png
Because of the Go strongly typed nature, the third line of code might be the most intrusive, and B contains the address of the a variable, but we want to increase the value stored in the A variable. So we have to dereference B, but follow the pointer by B to reference A.
Then we add the value 1 and store it back on the memory address stored in B.
The last line prints the value of a, and you can see that the value of a has been increased to 201
Image.png
Image.png
Conclusion
If you are a developer who comes from a development language that has no pointer concept or a pointer hidden in a variable, you need to create a model with a pointer-to-variable relationship, in short, remember this rule:
The pointer is a value that points to the memory address of another variable