Usage scenarios of pointers in go language?
If the function will modify the receiver, a pointer must be used at this time
If the receiver is a struct and contains a mutually exclusive type sync.Mutex, or a similar synchronization variable, the receiver must be Pointers, this can avoid object copy
If the receiver is a larger struct or array, it is more efficient to use pointers. How big is too big? Assume that all members in the struct must be passed in as function variables. If you feel that there is too much data at this time, the struct is too large
If the receiver is a struct, array or slice, and one of the elements points to an variable, then the receiver’s choice of pointer at this time will make the intention of the code more obvious
If the receiver is a smaller struct or array, and its variables are all invariants and constants, such as time.Time, value Receiver is more suitable because value receiver can reduce the amount of garbage that needs to be recycled.
How to understand the pointers behind the go language
The pointers in the Go language are the same as the C++ pointers, they point to the address of a certain block of memory. Values can be dereferenced. The only difference is that in C++ you can directly perform arithmetic operations on pointers but in Go you cannot.
tips: *, can point to the value at the pointer type memory address, number, can get the memory address of the value type
Each variable has a memory address, and the value in the memory address can be manipulated through the variable, that is, the size of the memory.
How to obtain the memory address of a variable in the go language: through symbols Get the address of a variable
Definition: Ordinary variables store values of corresponding types. These types are called value types
The address of variable b in memory is: 0x1040a124, in this The value stored at the memory address is: 156
Definition: Pointer type variables store an address, so they are called pointer types or reference types
b is a value type, It points to the value at the memory address
a is a pointer type, and it points to the memory address of b
Pointer type definition, syntax: var variable name*type
After the definition is completed, the pointer type defaults to a null address, that is, a null pointer (nil)
After the pointer variable is defined, the ***** symbol can be used to obtain the pointer pointed to by the pointer variable. Variable
Here *a is equivalent to b. By modifying *a, the value of value type b is ultimately modified
Here a, d are value types, b, c It is a pointer type
d is equivalent to adding a value to the memory address of a and creating a new space for storage in the memory. d and a do not affect each other
b and c are equivalent to pointing to When you use the * sign to reference the variable at the memory address and modify the value, the value of a will also change accordingly
Go language basics Grammar (1)
This article introduces some basic grammar of Go language.
Let’s first look at a simple go language code:
Go language annotation method:
Code execution result:
Next Further introduce the basic syntax of go.
For formatted output in the go language, you can use the two standard libraries fmt and log.
More formatting methods can be accessed from the fmt package.
The log package implements a simple log service and also provides some methods for formatting output.
Let’s introduce the data types of go
The following table lists the data types of go language:
Int, float, bool, string, array and struct are value types. Variables of these types directly point to values stored in memory; slice, map, chan, pointer, etc. are reference types and store an address. This address stores the final value.
Constants are values determined when the program is compiled and cannot be changed while the program is running.
Go language operators mainly include arithmetic operators, relational operators, logical operators, bitwise operators, Assignment operators and pointer-related operators.
Let’s introduce the if statement and switch statement in the go language. There is also a control statement called the select statement, which is usually used in conjunction with channels and will not be introduced here.
The syntax format of if is as follows:
if … else:
In addition, adding fallthrough will force the execution of subsequent case statements, regardless of whether the next case statement is true.
Here are several loop statements:
You can also exit the loop by marking:
The difference between go language structure variables and structure pointer variables
Mainly depends on the priority level, – has a higher priority than ++, ++p-num, because – has a higher priority, so Calculate p-num first, then num++, p++-num, calculate p-num first, then p++
GO article to understand the difference between pointers and address values
Pointers and address values in the Go language are often confusing in use, mainly due to their special * and symbols. Using it may make you confused. This article hopes to explain the pointer and value of the Go language clearly.
Here is a simple definition of the concepts of pointers and address values:
This is caused by the way the go method passes parameters. The go method function passes a copy of the parameters. , see what the following program will output?
The answer is 8, not 9, because the AddAge function modifies a backup of the student, not the original student object
If you want to correctly increase the age of the student, What the function needs to pass is the pointer of this value, as shown below:
It should be noted that the pointer we pass here is still a copy. For example, if you assign s to another pointer address, It will not affect the original pointer, you can practice this by yourself.
When developing in Go language, when should you use pointers and when should you use address values instead? For example, consider the following scenario:
Simple principle: When you are not sure which one to use, use pointers first
If you consider using pointers in composite objects such as arrays, slices, maps, etc. and value, such as:
Many developers will think that b will be more efficient, but what is passed is a copy of the slice, and the slice itself is a reference, so there is actually no difference in what is passed here.
Regarding the use of pointers and address values, one thing you need to keep in mind is the immutability of go data transfer. Learn and use this feature. This feature is very useful in stateless functions.