struct
We can declare new types as properties or field containers of other types.
For example, create a custom type person that represents an entity. This entity has attributes: name & age. We call this type struct.
type person struct{
name string
age int
}
var P person // P is now a variable of type person
= "Astaxie" // Assign "Astaxie" to P's name attribute.
= 25 // Assign "25" to the age attribute of variable P
("The person's name is %s", ) // Access P's name property.
In addition to the above P declaration usage, there are two other ways to use the declaration.
1. Provide initialization values in order
P := person{"Liuxinming", 28}
2. Initialize through field:value, so that it can be done in any order.
P := person{age:28, name:"Liuxinming"}
For example:
package main
import "fmt"
//Declare a new type
type person struct {
name string
avg int
}
//Compare the age of two people, return to the older person, and return to the age difference
//struct is also a value passed
func older(p1, p2 person) (person, int) {
if > { //Compare p1 and p2 ages
return p1, -
}
return p2, -
}
func main() {
var tom person
//Assignment initialization
, = "Tom", 18
//Initialization of both fields clearly written
bob := person{avg: 25, name: "Bob"}
//Initialize in order of struct definition
paul := person{"Paul", 43}
tb_Older, tb_diff := older(tom, bob)
tp_Older, tp_diff := older(tom, paul)
bp_Older, bp_diff := older(bob, paul)
("Of %s and %s, %s is older by %d years\n",
, , tb_Older.name, tb_diff)
("Of %s and %s, %s is older by %d years\n",
, , tp_Older.name, tp_diff)
("Of %s and %s, %s is older by %d years\n",
, , bp_Older.name, bp_diff)
}
The output result is as follows:
Of Tom and Bob, Bob is older by 7 years
Of Tom and Paul, Paul is older by 25 years
Of Bob and Paul, Paul is older by 18 years
Anonymous paragraph of struct
We have introduced above how to define a struct. When defining, the field name corresponds to its type one by one. In fact, Go supports only providing types and not writing field names, which is an anonymous field, also known as embedded fields.
When an anonymous field is a struct, all fields owned by this struct are implicitly introduced into the currently defined struct:
//
package main
import (
"fmt"
)
type Human struct {
name string
age int
weight int
}
type Student struct {
Human //Anonymous field, then the default struct contains all Human fields
speciality string
}
func main() {
//Initialize a student
mark := Student{Human{"Mark", 25, 100}, "Computer Science"}
//Access the corresponding fields
("His name is ", )
("His age is ", )
("His weight is ", )
("His speciality is ", )
//Modify the corresponding information
= "AI"
("Mark changed his speciality")
("His speciality is ", )
// Modify his age information
("Mark become old")
= 46
("His age is", )
// Modify his weight information
("Mark is not an athlet anymore")
+= 60
("His weight is", )
}
Output result:
His name is Mark
His age is 25
His weight is 100
His speciality is Computer Science
Mark changed his speciality
His speciality is AI
Mark become old
His age is 46
Mark is not an athlet anymore
His weight is 160