struct
-Define structure using type<name>struct{}, name follows visibility rules
-Supports pointer type members pointing to themselves
-Supports anonymous structures, action members, or defined member variables
-Anonymous structure can also be used for map values
-The structure can be initialized with literal values
-Allow values to read and write struct members through pointers
-members of the same type can make direct copy assignment
-Support = = with! = comparison operator, does not support > or <
-Supports anonymous fields, essentially a field defined with a type name
-Embedded structure as anonymous field looks like inheritance, but not inheritance
-Pointers for anonymous fields can be used
Package Main
Import "FMT"
Type Test struct{}
Func Main () {
A: =test{}
Fmt. Println (a)
}
Package Main
Import "FMT"
Type test struct {
Name string
Age int
Address string
}
Func Main () {
A: = test{}
A.name = "YH"//value initialization, similar to class in other languages, the go language does not have pointer arithmetic,
A.age = 18
Fmt. Printf ("My name is%s, this year's party%d\n", A.name, A.age)
B: = test{
Name: "YH",
Age:19,
Address: "Japan",
}
Fmt. Printf ("My name is%s, this year's party%d, I am%s\n", B.name, B.age, b.address)
}
Anonymous structure
Package Main
Import "FMT"
Type test struct {
Name string
Age int
Address string
Contact struct {
User_phont string
City string
}
}
Func Main () {
A: = Test{name: "YH", Age:19, Address: "Bj"}
A.contact.user_phont = "1234567890"
a.contact.city = "Haidian"
Fmt. Println (a)
}
Package Main
Import "FMT"
Func Main () {
A: = struct {
Name string
Age int
}{
Name: "AA",
Age:19,
}
Fmt. Println (a)
}
Package Main
Import "FMT"
Type Test struct{
Name string
Age int
}
Func Main () {
A: =test{
Name: "Coolqi",//Set literal value initialization
}
a.age=19//
Fmt. Println (a)
}
Package Main
Import "FMT"
Type Humen struct{
Sex int
}
Type Teacher struct{
Humen
Name string
Age int
}
Type Student struct{
Humen//The embedded is an anonymous field, essentially the name of the structure as the field name, if needed to initialize as a literal value, need to do humen:humen{sex:1}
Name string
Age int
}
Func Main () {
A: =teacher{name: "Joe", age:19,humen:humen{sex:1}}//in the embedded structure,
Fmt. Println (a)
}
This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1963454
Go language struct syntax