Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Go language pointer


May 11, 2021 Go


Table of contents


Go language pointer

Pointers are easy to learn in the Go language, and using pointers in the Go language makes it easier to perform tasks.

Let's take a step-by-step look at the Go language pointer.

As we all know, variables are easy-to-use placeholders that refer to computer memory addresses.

The Go language's address taker is the memory address of the variable, which is returned before it is placed in a variable.

The following example demonstrates the in-memory address of a variable:

package main

import "fmt"

func main() {
   var a int = 10   

   fmt.Printf("变量的地址: %x\n", &a  )
}

The above code output results in:

变量的地址: 20818a220

Now we've seen what a memory address is and how to access it. Next we'll cover pointers in detail.


What is a pointer

A pointer variable can point to the memory address of any value It points to the memory address of that value.

Similar to variables and constants, you need to declare pointers before you can use them. The pointer declaration format is as follows:

var var_name *var-type

Var-type is a pointer type, var_name name is a pointer variable, and the number is used to specify that the variable is a pointer. Here's a valid pointer declaration:

var ip *int        /* 指向整型*/
var fp *float32    /* 指向浮点型 */

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


How to use the pointer

Pointer usage process:

  • Define the pointer variable.
  • Assign a value to a pointer variable.
  • Access the value of the address in the pointer variable.

Add a sign (prefix) before the pointer type to get what the pointer points to.

package main

import "fmt"

func main() {
   var a int= 20   /* 声明实际变量 */
   var ip *int        /* 声明指针变量 */

   ip = &a  /* 指针变量的存储地址 */

   fmt.Printf("a 变量的地址是: %x\n", &a  )

   /* 指针变量的存储地址 */
   fmt.Printf("ip 变量的存储地址: %x\n", ip )

   /* 使用指针访问值 */
   fmt.Printf("*ip 变量的值: %d\n", *ip )
}

The output of the above examples is:

a 变量的地址是: 20818a220
ip 变量的存储地址: 20818a220
*ip 变量的值: 20

Go empty pointer

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

The nil pointer is also known as an empty pointer.

nil, like null, None, nil, NULL in other languages, refers to zero or null values conceptually.

A pointer variable is usually abbreviated as ptr.

See the following examples:

package main

import "fmt"

func main() {
   var  ptr *int

   fmt.Printf("ptr 的值为 : %x\n", ptr  )
}

The output of the above examples is:

ptr 的值为 : 0

Empty pointer judgment:

if(ptr != nil)     /* ptr 不是空指针 */
if(ptr == nil)    /* ptr 是空指针 */

Go pointer more

Next we'll introduce you to more pointer applications in the Go language:

Content Describe
An array of Go pointers You can define an array of pointers to store addresses
Go's pointer to the pointer Go supports pointers to pointers
Go passes pointer arguments like a function You can change the value of a function when it is called by reference or address reference