Go
pointers are easy to learn in a language
Go
using pointers in the language makes it easier to perform some tasks.
Next, let’s learn
Go
language pointer step by step.
As we all know, a variable is an easy-to-use placeholder for referencing thecomputer’s memory address.
The address character of the
Go
language is
&
, when used in front ofa variable, the memory address of the corresponding variable is returned.
The following example demonstrates the in-memory address of a variable: The output result of executing the above code is: Now we know what the memory address is and how to access it. Next we will introduce pointers in detail. A pointer variable points to the memory address of a value. Similar to variables and constants, you need to declare pointers before using them. The pointer declaration format is as follows: In this case, this is a point to Pointer usage process: Define pointer variables. Assign a value to the pointer variable. Access the value in the pointer variable that points to the address. Add before the pointer type The output of the above example is as follows: When a pointer is defined and not assigned to any variable, its value is A pointer variable is usually abbreviated to View the following examples: The output result of the above example is: Null pointer judgment: Next, we will introduce to you Content Description Go pointer array You can define an array of pointers to store addresses. A pointer to a pointer by a Go Go supports pointers to pointers Go passes pointer parameters to the function By passing parameters by reference or address, you can change the value of afunction when it is called 2.32.1. Example #
package main
import "fmt"
func main() {
var a int = 10
fmt.Printf("variable address: %x\\n", &a )
}
variable address: 20818a220
What is a pointer? #
var var_name *var-type
var-type
is the pointer type,
var_name
is the pointer variable name``*`` is used to specify that the variable is used as a pointer. The following are valid pointer declarations:var ip *int /* Pointing integer*/
var fp *float32 /* Pointing to floating point type */
int
and
float32
pointer.How to use pointers #
*
(prefix) to get what the pointer points to. 2.32.2. Example #
package main
import "fmt"
func main() {
var a int= 20 /* Declare actual variables */
var ip *int /* Declare pointer variables */
ip = &a /* Storage address of pointer variable */
fmt.Printf("The address of variable a is: %x\\n", &a )
/* Storage address of pointer variable */
fmt.Printf("ip Pointer address for variable storage: %x\\n", ip )
/* Using pointers to access values */
fmt.Printf("*ip Value of variable: %d\\n", *ip )
}
The address of variable a is: 20818a220
The pointer address stored in the IP variable: 20818a220
*The value of the IP variable: 20
Go null pointer #
nil
.
nil
pointer is also called a null pointer.
nil
conceptually and in other languages
null
、
None
、
nil
、
NULL
similarly, they all refer to zero or null values.
ptr
. 2.32.3. Example #
package main
import "fmt"
func main() {
var ptr *int
fmt.Printf("The value of ptr is: %x\\n", ptr )
}
The value of ptr is: 0
if(ptr != nil) /* Ptr is not a null pointer */
if(ptr == nil) /* Ptr is a null pointer */
Go pointer more #
Go
more pointer applications in the language: