In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to use structures in the Go language. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Structural body
The basic data types in Go language can represent the basic properties of some things, but when we want to express all or part of the attributes of a thing, the basic data types can not meet the requirements.
The Go language provides a custom data type that can encapsulate multiple basic data types, which are called struct.
We can define our own types through struct.
Object-oriented is realized by struct in Go language.
2.1 definition of structures
Use the type and struct keywords to define the structure, as shown in the following code format:
Type type name struct {
Field name Field Type
Field name Field Type
...
}
Where:
Type name: identifies the name of the custom structure, which cannot be repeated within the same package.
Field name: represents the structure field name. The field name in the structure must be unique.
Field type: represents the specific type of the structure field.
For example, let's define a Person structure with the following code:
Type Person struct {
Name string
City string
Age int8
}
Fields of the same type can be unloaded by a row
Type Person struct {
Name,city string
Age int8
}
So we have a custom type of Person, which has three name,city,age fields that represent the name, city, and age.
In this way, we can use the Person structure to store human information.
The built-in data type of the language is used to describe a worthwhile, and the structure is used to describe a set of values.
For example, a person with a name, age and city of residence is essentially an aggregated data type.
2.2 structural body instantiation
Memory is actually allocated only when the structure body is instantiated. That is, the field of the structure must be instantiated before it can be used.
The structure itself is a type, and we can declare the structure type using the var keyword just like the built-in type.
Var structure instance structure type
2.2.1 basic instantiation of structure
Pass "." To access the fields of the structure, such as person1.name and person1.age
Package mainimport "fmt" type Person struct {name,city string age int8} func main () {var person1 Person person1.name= "vita" person1.city= "ShangHai" person1.age=27 fmt.Printf ("p1 percent v\ n", person1) fmt.Printf ("p1 percent percent v\ n", person1)} result: p1 = {vita ShangHai 27} p1=main.Person {name: "vita", city: "ShangHai", age:27} Process finished with exit code 0
2.2.2 Anonymous structure
Anonymous structures can be used when defining temporary data structures.
Package mainimport "fmt" func main () {var person2 struct {name string;age int} person2.name= "vita" person2.age=27 fmt.Printf ("p1% v\ n", person2) fmt.Printf ("p1% roomv\ n", person2)} result: p1 = {vita 27} p1=struct {name: "vita", age:27} Process finished with exit code 0
2.2.3 create a pointer type structure
We can also instantiate the structure with the new keyword to get the address of the structure.
Package main
Import "fmt"
Type Person struct {
Name string
City string
Age int
}
Func main () {
Var person3 = new (Person)
Fmt.Printf ("% T\ n", person3)
Fmt.Printf ("p3% roomv\ n", person3)
Person3.name= "vita"
Person3.age=27
Fmt.Printf ("p3% v\ n", person3)
Fmt.Printf ("p3% roomv\ n", person3)
}
Results:
* main.Person
P3=&main.Person {name: ", city:", age:0}
P3percent & {vita 27}
P3=&main.Person {name: "vita", city: "", age:27}
Process finished with exit code 0
As you can see from the print result, p3 is a structure pointer.
The Go language supports the direct use of "." for struct pointers. Access structure members.
2.2.4 address instantiation of fetch structure
Using & to address a structure is equivalent to an new instantiation of the structure.
Package mainimport "fmt" type Person struct {name string city string age int} func main () {var person4 = & Person {} fmt.Printf ("% T\ n", person4) fmt.Printf ("p4 percent v\ n", person4) person4.name= "vita" person4.age=27 fmt.Printf ("p4 percent percent v\ n", person4) fmt.Printf ("p4 percent roomv\ n", person4)} result: * main.Personp4=&main.Person {name: ", city:" Age:0} p4others & {vita 27} p4=&main.Person {name: "vita", city: "", age:27} Process finished with exit code 0
Person4.name= "vita" is actually (* person4) .name = "vita" at the bottom, which is the syntax candy that the Go language helps us implement.
The above is how to use structures in Go language. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.