Pointers in Go
Pointers in Go are easy to learn, and using pointers in Go makes it simpler to perform some tasks.
Next, let's learn Go pointers step by step.
As we all know, a variable is a convenient placeholder used to reference a memory address in a computer.
The address-of operator in Go is &; placed before a variable, it returns the memory address of the corresponding variable.
The following example demonstrates the address of a variable in memory:
Example
package
main
import
"fmt"
func
main
()
{
var
a
int
=
10
fmt
.
Printf
(
"address of the variable: %x\n"
,
&
a
)
}
The output of the code above is:
变量的地址: 20818a220
Now that we understand what a memory address is and how to access it, we will introduce pointers in detail next.
What is a pointer
A pointer variable points to the memory address of a value.
Similar to variables and constants, you need to declare a pointer before using it. The pointer declaration format is as follows:
var var_name *var-type
var-type is the pointer type, var_name is the pointer variable name, and the * sign is used to specify that the variable is a pointer. The following are valid pointer declarations:
var ip *int /* 指向整型*/
var fp *float32 /* 指向浮点型 */
In this example, this is a pointer to int and float32.
How to use pointers
The process of using a pointer:
- Define a pointer variable.
- Assign a value to the pointer variable.
- Access the value at the address pointed to by the pointer variable.
Add a * sign (prefix) before the pointer type to get the content pointed to by the pointer.
Example
package
main
import
"fmt"
func
main
()
{
var
a
int
=
20
/* declare the actual variable */
var
ip
int
/* declare the pointer variable */
ip
=
&
a
/* the storage address of the pointer variable */
fmt
.
Printf
(
"address of variable a is: %x\n"
,
&
a
)
/* the storage address of the pointer variable */
fmt
.
Printf
(
"pointer address stored in variable ip: %x\n"
,
ip
)
/* access the value using the pointer */
fmt
.
Printf
(
"value of *ip variable: %d\n"
,
ip
)
}
The output of the above example is:
a 变量的地址是: 20818a220
ip 变量储存的指针地址: 20818a220
*ip variable value: 20
Go nil pointer
When a pointer is defined but not assigned to any variable, its value is nil.
A nil pointer is also called a null pointer.
Conceptually, nil is the same as null, None, nil, and NULL in other languages, all referring to a zero value or empty value.
A pointer variable is usually abbreviated as ptr.
See the following example:
Example
package
main
import
"fmt"
func
main
()
{
var
ptr
int
fmt
.
Printf
(
"value of ptr is : %x\n"
,
ptr
)
}
The output of the above example is:
ptr 的值为 : 0
Null pointer check:
if(ptr != nil) /* ptr 不是空指针 */
if(ptr == nil) /* ptr 是空指针 */
More about Go pointers
Next, we will introduce more pointer applications in Go:
| Content | Description |
|---|---|
| Go pointer arrays | You can define an array of pointers to store addresses |
| Go pointer to a pointer | Go supports pointers to pointers |
| Go passing pointer parameters to functions | By passing by reference or address, the value can be changed during the function call |