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

The method of structure pointer Operation in go language

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "the method of structure pointer operation in go language". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Pointer

A pointer is a value that represents a memory address. The memory address stores the value of another variable.

Pointer (address), once unchangeable is defined, the value that the pointer points to can be changed

Go pointer operation

1. Default value nil, no NULL constant

two。 The operator "&" takes the variable address, and "*" accesses the target object (pointing to the value) through the pointer (address).

3. Does not support pointer operation, does not support the "- >" (arrow) operator, directly use "." Access to target members

Example 1:

Package main / / must have a main package import "fmt" func main () {var an int = 10 / / each variable has two meanings: the memory of the variable, the address of the variable fmt.Printf ("a =% d\ n", a) / the memory fmt.Printf of the variable ("& a =% d\ n", & a) / the address to hold a variable The pointer type * int saves the address of int, and * * int saves * int address / / declaration (definition). The definition is just a special declaration / / definition of a variable p, and the type is * int var p * int p = & a / / whom the pointer variable points to. Assign the address to the pointer variable fmt.Printf ("p =% v, & a =% v\ n", p, & a) * p = 666 / / * p operates not the memory of p, but the memory (that is, a) fmt.Printf ("* p =% v, a =% v\ n", * p, a)}

Example 2:

Package mainimport "fmt" func main () {a: = 10b: = & a * b = 11111 / / the value of operation pointing to a fmt.Println (a) / / 11111} cannot operate illegally pointing to package main / / there must be a main packet import "fmt" func main () {var p * int p = nil fmt.Println ("p =", p) / / * p = 666 / / err Because p does not have a legal point to var an int p = & a / / p to a * p = 666 fmt.Println ("a =", a)} new function

The expression new (int) creates an anonymous variable of type int, allocates and clears a piece of memory for a new value of type int, and then returns the address of that memory space as a result, which is the pointer value pointing to the new int type value with a pointer type of * int.

Package mainimport "fmt" func main () {/ / var a * int a: = new (int) / / an is * int, pointing to anonymous int variable fmt.Println (* a) / / 0b: = new (int) / / b is * int, and pointing to anonymous int variable * b = 2 fmt.Println (* b) / / 2}

We just need to use the new () function without worrying about the life cycle of memory and recycling deletions. Because the GO language (gc) memory management system will help us deal with it.

The pointer acts as a parameter to the function

Example 1: exchange values, ordinary variables as function parameters. Internal exchange successful, external failure

Package main / / must have a main package import "fmt" func swap (a, b int) {a, b = b, a fmt.Printf ("swap: a =% d, b =% d\ n", a, b) / swap: a = 20, b = 10} func main () {a, b: = 10,20 / / Exchange an and b contents swap (a) through a function B) / / the variable itself is passed Value passed (from a variable perspective) fmt.Printf ("main: a =% d, b =% d\ n", a, b) / / main: a = 10, b = 20}

Example 2: the pointer passes parameters, and the internal and external exchanges are successful

Package main / / must have a main package import ("fmt") func test (a, b * int) {* a, * b = * b, * a fmt.Printf ("swap: a =% d, b =% d\ n", * a, * b)} func main () {a, b: = 10,20 / exchange an and b contents test (& a) through a function & b) fmt.Printf ("main: a =% d, b =% d\ n", a, b)} array pointer

/ / (* p) [0] = 666 array pointer assignment

Package main / / there must be a main package import "fmt" / / p pointing to the implementation array a, which points to the array, which is the memory that the array pointer / / * p represents the pointer to. Is the argument afunc modify (p * [5] int) {(* p) [0] = 666fmt.Println ("modify * a =", * p) / / modify * a = [6662345]} func main () {a: = [5] int {1,2,3,4,5} / initialize modify (& a) / / address passing fmt.Println ("main: a =") A) / / modify * a = [666 2 3 45]} structure pointer variable package main / / must have a main package import "fmt" / / define a structure type type Student struct {id int name string sex byte / / character type age int addr string} func main () {/ / sequence initialization Each member must be initialized. Don't forget & var p1 * Student = & Student {1, "mike", 'masks, 18, "bj"} fmt.Println ("p1 =", p1) / / p1 = & {1 mike 109 18 bj} / / specify member initialization, no initialization member Automatically assigned to 0p2: = & Student {name: "mike", addr: "bj"} fmt.Printf ("p2 type is% T\ n", p2) / / p2 type is * main.Student fmt.Println ("p2 =" P2) / / p2 = & {0 mike 0 bj}} structure member ordinary variable / / define a structure type type Student struct {id int name string sex byte / / character type age int addr string} func main () {/ / define a structure ordinary variable var s Student / / operator member You need to use a point (.) Operator s.id = 1 s.name = "mike" s.sex ='m' / / character s.age = 18 s.addr = "bj" fmt.Println ("s =", s) / / s = {1 mike 109 18 bj}} structure member pointer variable func main () {/ / 1, after the pointer has a legal point Before operating member / / define a common structural variable var s Student / / define a pointer variable, save the address of s var p1 * Student p1 = & s / / manipulate member p1.id and (* p1) .id through the pointer is completely equivalent and can only be used. Operator p1.id = 1 (* p1). Name = "mike" p1.sex ='m 'p1.age = 18 p1.addr = "bj" fmt.Println ("p1 =" P1) / / 2. Apply for a structure through new p2: = new (Student) p2.id = 1 p2.name = "mike" p2.sex ='m 'p2.age = 18 p2.addr = "bj" fmt.Println ("p2 =", p2)} structure comparison and assignment func main () {S1: = Student {1, "mike",' m' "bj"} test02 (& s) / / address passing (reference passing) Parameter can not change the actual parameter fmt.Println ("main:", s)} "go language structure pointer operation method" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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