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

Example Analysis of Go struct instantiation and assignment

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "Go struct instantiation and assignment instance analysis". In daily operation, I believe many people have doubts about Go struct instantiation and assignment instance analysis. The editor consulted all kinds of materials and sorted out simple and useful operation methods, hoping to help you answer the doubts of "Go struct instantiation and assignment instance analysis". Next, please follow the editor to study!

There are various instantiation and assignment methods of struct in Golang, one moment is the value transfer, the other is the pointer, which is confusing, so I decided to sort it out and understand the whole.

First define a structure, the following combined with the code to explain.

Package mainimport "fmt" type Person struct {Name string Age int Descprtion string} instance 1

P instantiates a struct in the most conventional way, and the variable p gets a Person structure.

P: = Person {} p.Name = "Xiaoming" fmt.Printf ("pvv% var address:% p\ n", p, & p) fmt.Println ("=") / / result:// p: {Name: Xiaoming Age:0 Descprtion:} variable address: 0xc000078480// = instance 2

The variable p1 is assigned by p. Because the Golang language is value transfer, the modification of p1 will not affect p after assignment.

You can also see from the first output that there is no write-time copy (copy on write) mechanism for Golang assignment like the PHP variable assignment.

P1: = pfmt.Printf ("p1VV variable address:% p\ n", p1, & p1) / / copy p1.Name = "Xiaoming p1" fmt.Printf when writing ("p1% Variv address:% p\ n", p, & p) fmt.Printf ("p1VV variable address:% p\ n", p1 & p1) fmt.Println ("=") / / result:// p1: {Name: Xiaoming Age:0 Descprtion:} variable address: 0xc0000784e0// p: {Name: Xiaoming Age:0 Descprtion:} variable address: 0xc000078480// p1: {Name: Xiaoming p1 Age:0 Descprtion:} variable address: 0xc0000784e0// = instance 3

The address of p is assigned to p2 using an address character, and the variable p2 is a pointer that holds the address to p. When p2 modifies the element Name in the structure, the corresponding value of the structure accessed by p changes accordingly.

P2: = & p / / is equivalent to var p2 * Person = & pfmt.Printf ("p2VV pointer variable points to address (variable value):% p variable address:% p\ n", p2, p2, & p2) p2.Name = "Xiaoming p2" fmt.Printf ("p1purr% Secretv variable address:% p\ n", p, & p) fmt.Printf ("p2VAR% roomv pointer variable pointing address (variable value):% p variable address:% p\ n" P2, p2, & p2) fmt.Println ("=") / result:// p2Age:0 Descprtion & {Name: Xiao Ming Age:0 Descprtion:} pointer variable points to address (variable value): 0xc000078480 variable address: 0xc000006030// p1: {Name: Xiao Ming p2 Age:0 Descprtion:} variable address: 0xc000078480// p2 Age:0 Descprtion & {Name: Xiao Ming p2 Age:0 Descprtion:} pointer variable pointing address (variable value): 0xc000078480 variable address: 0xc000006030// = instance 4

The variable p3 is obtained by new (Person). New will open up a piece of memory and return the memory address to p3, that is, p3 is a pointer to this piece of memory.

P3 is a pointer to the structure. There are two ways to manipulate the structure, p3.Age = 3 and * p3 = Person {Name: "Xiao Ming p3"}. If you operate in the second way, it will overwrite the modification of the structure in the first way.

Because p3 is a pointer, when p3 is assigned to p5, p5 will also point to this memory address.

P3: = new (Person) fmt.Printf ("p3PVV pointer variable points to address (variable value):% p variable address:% p\ n", p3, p3, & p3) p3.Age = 3 / / equivalent to (* p3). Age = 3fmt.Println ("= operation Age =") fmt.Printf ("p3VAR% variv pointer variable points to address (variable value):% p variable address:% p\ n", p3, p3 & p3) * p3 = Person {Name: "Xiao Ming p3",} fmt.Println ("= operation Name =") fmt.Printf ("p3fmt.Println% variv pointer variable points to address (variable value):% p variable address:% p\ n", p3, p3, & p3) p5: = p3fmt.Println ("= p5: = p3 =") fmt.Printf ("p3fmt.Println% variv pointer variable points to address (variable value):% p variable address:% p\ n", p3 P3, & p3) fmt.Printf ("p5fmt.Println% variv pointer variable points to address (variable value):% p variable address:% p\ n", p5, p5, & p5) p3.Name = "Xiaoming p3 modification" fmt.Println ("= p3 modification =") fmt.Printf ("p3% variv pointer variable points to address (variable value):% p variable address:% p\ n", p3, p3 & p3) fmt.Printf ("p5VR% variv pointer variable points to address (variable value):% p variable address:% p\ n", p5, p5 & p5) fmt.Println ("=") / / result:// p3Age:0 Descprtion & {Name: Age:0 Descprtion:} pointer variable points to address (variable value): 0xc000078630 variable address: 0xc000006038// = Operation Age = / / p3Age:0 Descprtion & {Name: Age:3 Descprtion:} pointer variable points to address (variable value): 0xc000078630 variable address: 0xc000006038// = Operation Name = / / p3 Age:0 Descprtion & {Name: Xiaoming p3 Age:0 Descprtion:} Pointer variable points to address (variable value): 0xc000078630 variable address: 0xc000006038// = p5: = p3 = / / p5 Age:0 Descprtion & {Name: Xiao Ming p3 Age:0 Descprtion:} pointer variable point address (variable value): 0xc000078630 variable address: 0xc000006040// = p3 modification = / / p3Age:0 Descprtion & {Name: Xiao Ming p3 modification Age:0 Descprtion:} pointer variable point address (variable value): pointer variable address: 0xc000006038// p5: & {Name: Xiaoming p3 modified Age:0 Descprtion:} pointer variable points to address (variable value): 0xc000078630 variable address: 0xc000006040// = example 5

The instantiation of p4 will also get a pointer, which is the same as the instantiation of p3, but the writing of p4 is more commonly used.

P4: = & Person {Name: "Xiaoming p4",} fmt.Printf ("% + v% p\ n", p4, & p4) / / result:// & {Name: Xiaoming p4 Age:0 Descprtion:} 0xc000006048

Complete code is attached:

Package mainimport "fmt" type Person struct {Name string Age int Descprtion string} func main () {p: = Person {} p.Name = "Xiaoming" fmt.Printf ("pvv variable address:% p\ n", p, & p) fmt.Println ("=") p1: = p fmt.Printf ("p1PVV variable address:% p\ n", p1 & p1) / / copy when writing p1.Name = "Xiaoming p1" fmt.Printf ("pvv variable address:% p\ n", p, & p) fmt.Printf ("p1VR% transiv variable address:% p\ n", p1 & p1) fmt.Println ("=") p2: = & p fmt.Printf ("p2VV pointer variable points to address (variable value):% p variable address:% p\ n", p2, p2, & p2) p2.Name = "Xiao Ming p2" fmt.Printf ("p1VV variable address:% p\ n", p & p) fmt.Printf ("p2new% variv pointer variable points to address (variable value):% p variable address:% p\ n", p2, p2, & p2) fmt.Println ("=") p3: = new (Person) fmt.Printf ("p3PVV pointer variable points to address (variable value):% p variable address:% p\ n", p3, p3 & p3) p3.Age = 3 / / is equivalent to (* p3). Age = 3 fmt.Println ("= operation Age =") fmt.Printf ("p3fmt.Println% pointer variable to address (variable value):% p variable address:% p\ n", p3, p3, & p3) * p3 = Person {Name: "Xiao Ming p3" } fmt.Println ("= operation Name =") fmt.Printf ("p3fmt.Printf% variv pointer variable points to address (variable value):% p variable address:% p\ n", p3, p3, & p3) p5: = p3 fmt.Println ("= p5: = p3 =") fmt.Printf ("p3fmt.Printf% transiv pointer variable points to address (variable value):% p variable address:% p\ n", p3, p3 & p3) fmt.Printf ("p5fmt.Println% modiv pointer variable points to address (variable value):% p variable address:% p\ n", p5, p5, & p5) p3.Name = "Xiaoming p3 modification" fmt.Println ("= p3 modification =") fmt.Printf ("p3VV pointer variable pointing address (variable value):% p variable address:% p\ n", p3, p3, p3 & p3) fmt.Printf ("p5 Name% variv pointer variable points to address (variable value):% p variable address:% p\ n", p5, p5, & p5) fmt.Println ("=") p4: = & Person {Name: "Xiao Ming p4",} fmt.Printf ("% + v% p\ n", p4, & p4)} so far The study on "Go struct instantiation and assignment instance analysis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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