2.32. Go language pointer

发布时间 : 2025-10-25 13:32:52 UTC      

Page Views: 9 views

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:

2.32.1. 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.

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 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 null None nil NULL similarly, they all refer to zero or null values.

A pointer variable is usually abbreviated to ptr .

View the following examples:

2.32.3. 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

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。