Hello, I am discrete. 🌞
Today I’m going to share with you when it’s appropriate to use Pointers.
What is a pointer
We often hear Golang being passed by value, so-and-so being passed by reference, so-and-so being passed by pointer, and so on.
So what’s the difference between them in the first place? What is a pointer? A pointer is actually a variable, but instead of an int, float, struct, it’s an address, and the data stored in that address can be read by the pointer.
OK, pointer variable stores an address, where does the address come from? You have to ask how do you get the address of a variable? Just put an ampersand in front of the variable.
Ok, the pointer variable stores the address, so how can the value stored at the address be read? How do I get the value that the pointer points to? A pointer variable is preceded by an *.
How do I modify the data that the pointer points to? Just add the * to the front and assign a new value.
func main(a) {
a := "Lisanaaa"
b := &a
fmt.Println("The value of a is:", a)
fmt.Println("The address of a is:", &a)
fmt.Println("The value of b is:", b)
fmt.Println("Before, the value b points to is:", *b)
*b = "not Lisanaaa"
fmt.Println("After, the value b points to is:", *b)
}
Copy the code
The output is:
The value of a is: Lisanaaa
The address of a is: 0xc00000e1e0
The value of b is: 0xc00000e1e0
Before, the value b points to is: Lisanaaa
After, the value b points to is: not Lisanaaa
Copy the code
Function of pointer
Why do we have Pointers? What is its key function? Let’s look at the following code:
type Lisanaaa struct {
Description string
}
func main(a) {
a := Lisanaaa{
Description: "niubi",
}
fmt.Println("Before, the description of Lisanaaa is:", a.Description)
modify(a)
fmt.Println("After, the description of Lisanaaa is:", a.Description)
}
func modify(a Lisanaaa) {
a.Description = "laji"
}
Copy the code
The output is
Before, the description of Lisanaaa is: niubi
After, the description of Lisanaaa is: niubi
Copy the code
Why did I change description of A, but not Descriptipn?
When we passed in the parameter, the A was copied, that is to say, there are two AS now, one is a of main and the other is a of modify. If the modify function changes its A, the A in main will not be affected.
So how do we change the main variable inside our modify function? We can do this by passing in a pointer.
type Lisanaaa struct {
Description string
}
func main(a) {
a := Lisanaaa{
Description: "niubi",
}
fmt.Println("Before, the description of Lisanaaa is:", a.Description)
modify(&a)
fmt.Println("After, the description of Lisanaaa is:", a.Description)
}
func modify(a *Lisanaaa) {
a.Description = "laji"
}
Copy the code
The output is:
Before, the description of Lisanaaa is: niubi
After, the description of Lisanaaa is: laji
Copy the code
This also teaches us a truth, only we can call ourselves laji, others can not change us. Please be yourself! Always on the way!
Seeing the above results, we can not help but ask ourselves a question, pass pointer is not copy? No, we’re going to copy it, but we’re just going to copy this pointer variable, instead of copying the whole of A like we did before.
So what Pointers do:
-
Pointers not only help functions change the value of external variables internally, but also help us change the value of the data they point to anywhere.
-
Passing pointer parameters saves the memory overhead of copying large structures.
When is it appropriate to use Pointers
-
If we need to change the value of an external variable, we need to use Pointers;
-
There is no need to use Pointers to map, slice, etc., because they are Pointers themselves, which I will share in the next article.
-
If you have super-large structures that need to be used as arguments to a function, using Pointers can save memory overhead.
-
Because Pointers can change the value they point to, it is best not to use them arbitrarily in concurrent scenarios;
-
It is best not to use nested Pointers, such as b := &a, c := &b, as this code can be complicated and difficult to understand.
conclusion
-
Pointers are also variables, but store addresses;
-
Pointers can be used to modify the value of the data they point to.
-
Pointers can help us change the value of the data they point to anywhere;
-
Passing pointer parameters saves the memory overhead of copying large structures.
-
See the section above for when to use the pointer.
I hope readers can learn something from this article. May I be as rich as my Homie.
About the author:
Lisa Aaa, an Internet practitioner, loves algorithms and system design, and enjoys playing basketball and fitness.
- I have read 2k+ resumes, provided resume consulting to more than 100 people, and helped them get the interview successfully.
- Once led the Github open source algorithm project, with 1W + STAR;
- Has written his own algorithm column and has over 2K + subscriptions;
- Crossfire ranked seventh in jiangxi division in the first season, haven’t played for a long time, welcome to find me about the game.