Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pointers in Go language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use pointers in Go language". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it to study and learn "how to use pointers in Go language" together!

What's a pointer?

As we all know, the data stored in the program is stored in memory, and each data stored in memory has a number, which is the memory address. We can find data stored in memory based on this memory address, which can be assigned to a pointer. We can also simply understand that pointers are memory addresses.

Declaration and definition of pointers

In Go, to get a pointer, use the address symbol & directly.

Examples:

func main() { name := "micro-guest nest" nameP := &name //Take address fmt.Println("name variable value is: ", name) fmt.Println("name variable memory address is: ", nameP)}//Run result://name variable value is: micro-guest nest//name variable memory address is: 0xc 0004 e240

The type of nameP pointer is *string

In Go, a * type name denotes a corresponding pointer type.

variable memory data memory address name := "micro-guest bird nest" micro-guest bird nest 0xc00004e240 nameP :=&name0xc0004e2400xc0004e360

From the table above you can see:

The value of the common variable name is micro-guest bird nest, stored in memory address 0xc00004e240

The value of pointer variable namep is the memory address of ordinary variable 0xc00004e240

The value of pointer variable nameP is stored in memory at memory address 0xc00004e360

Ordinary variables store data, pointer variables store addresses of data.

var keyword declaration

We can also use the var keyword declaration

var nameP *stringnameP = &namenew function declaration

nameP := new(string)nameP = &name

You can pass a type to this built-in new function, which returns the corresponding pointer type.

pointer operation

Here's the emphasis:

A pointer variable is a variable whose value is a pointer (memory address)!

A pointer variable is a variable whose value is a pointer (memory address)!

A pointer variable is a variable whose value is a pointer (memory address)!

Get the value the pointer points to:

Just add * to pointer variable to get the data corresponding to pointer variable value:

nameV := *namePfmt.Println("nameP pointer points to value:",nameV) //nameP pointer points to value: microguest nest

Modify the value the pointer points to:

*nameP = "" //modify the pointer to the value fmt.Println("nameP pointer to the value:",*nameP)fmt.Println("name variable value:",name)//Run result://nameP pointer to the value: //name variable value:

We find that the nameP pointer points to a value that has changed, and the variable name has changed.

Because the memory where the variable name stores data is the memory pointed to by the pointer nameP, after this memory is modified by nameP, the value of the variable name is also modified.

A pointer variable defined directly by the var keyword cannot be assigned because its value is nil, that is, it does not point to a memory address.

//error example var intP *int*intP = 10 //error, should first allocate a block of memory, memory address as the value of the variable intP, this memory can store 10.// var intP *int //should be used to declare int-type pointer variables intPintP = new(int) //allocate a block of memory for pointers *intP = 66 fmt.Println("::",intP) //:: 0xc000ac088fmt.Println (*intP) //66//short var intP := new(int)*intP=66 pointer parameters

When a pointer is used as an argument to a function, it is possible to change the value of the argument in the function through the parameter:

func main() { name := "dust-free" modify(&name) fmt.Println("name has the value:",name)}func modify(name *string) { *name = "wucs"}//Run result://name has the value: wucs pointer receiver

If the receiver type is a reference type such as map, slice, channel, etc., pointer is not used;

If you need to modify the receiver, you need to use pointers;

If the receiver is of a larger type, consider using pointers, since memory copying is inexpensive and therefore efficient.

When to use pointers

Do not use pointers to reference types such as map, slice, and channel;

Pointers are needed if you need to modify data or state inside the method receiver;

If you need to modify the parameter value or internal data, you also need to use pointer type parameters;

If it is a relatively large structure, each time the parameter is passed or the method is called, the memory is copied, and the memory is occupied. At this time, you can consider using pointers.

Pointers are unnecessary for small data types like int and bool;

If concurrency safety is required, do not use pointers as much as possible, and use pointers to ensure concurrency safety;

Pointers should not be nested, meaning you should not use a pointer to a pointer, although Go allows this, but it can make your code extremely complex.

Thank you for reading, the above is "how to use pointers in Go language" content, after the study of this article, I believe that we have a deeper understanding of how to use pointers in Go language, the specific use of the situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report