Go language pointer


Release date:2023-09-16 Update date:2023-10-13 Editor:admin View counts:298

Label:

Go language pointer

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:

Example

package main
import "fmt"
func main() {
   var a int = 10
   fmt.Printf("variable address: %x\\n", &a  )
}

The output result of executing the above code is:

variable address: 20818a220

Now we know what the memory address is and how to access it. Next we will introduce pointers in detail.

What is a pointer?

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:

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 */

In this case, this is a point to int and float32 pointer.

How to use pointers

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 * (prefix) to get what the pointer points to.

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 output of the above example is as follows:

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

When a pointer is defined and not assigned to any variable, its value is nil .

nil pointer is also called a null pointer.

nil conceptually and in other languages nullNonenilNULL similarly, they all refer to zero or null values.

A pointer variable is usually abbreviated to ptr .

View the following examples:

Example

package main
import "fmt"
func main() {
   var  ptr *int
   fmt.Printf("The value of ptr is: %x\\n", ptr  )
}

The output result of the above example is:

The value of ptr is: 0

Null pointer judgment:

if(ptr != nil)     /* Ptr is not a null pointer */
if(ptr == nil)    /* Ptr is a null pointer */

Go pointer more

Next, we will introduce to you Go more pointer applications in the language:

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

Powered by TorCMS (https://github.com/bukun/TorCMS).