This is a creation in Article, where the information may have evolved or changed.
First, the definition of struct structure:
Type person struct {name stringage int}
This defines the person structure, name is the string type name, and age is an int type.
Second, declare a struct variable:
var p person
Three, the assignment value:
There are three ways to do this, respectively:
1, directly assigned value
P.name = "Xiaoming" P.age = 12
2. Initialize values in sequence
Tom: = person{"Tom", 20}
3, through the way of field:value initialization, so that the order can be arbitrary
John: = person{age:21, Name: "John"}
Iv. Anonymous fields for structs
Go support provides only field types, and does not provide a way to declare field names. That is, an anonymous field, also called an embedded field. (Can be understood by inheritance in Java)
When an anonymous field is a struct (or other custom type), all the fields owned by the struct are implicitly introduced into the currently defined struct.
As an example:
Type Human struct{ name string age int}type Student struct{ Human//anonymous field, then Student has all the fields of Human Score Int}func Main () { var xiaoming = student{human{"Xiaoming", +}, 98} FMT. Println (xiaoming. Human.name); Fmt. Println (xiaoming. Human.age); Fmt. Println (Xiaoming.score);}