This is the 10th day of my participation in the August Text Challenge.More challenges in August

struct

A structure is a compound type, a value type, and can be created by the new function, which returns a pointer to the structure

Consists of a series of fields, each with its own type and field name, which follows visibility rules

format

Struct {struct 1; struct 2; struct 2;Copy the code

Defining structure

Type Person struct {// 表 1 name string 'name' // 表 2 age int 'age'}Copy the code

Struct creation

P1 := Person{"shaosiming", 18} FMT.Println(p1) p2 := Person{age: 26, name: "dasiming"} fmt.println (p2) // Create a structure with a pointer to the structure and set the fields in the structure to zero p3 := new(Person) fmt.println (p3)Copy the code

Struct access

// Struct variable name. P4 := Person{"shaosiming", 18} FMT.Println(p4.name) FMT.Println(p4.age)Copy the code

Tags in a structure

Each field in a structure can have a tag, which is a string, in addition to a name and type.

Tags in the structure can be retrieved with Reflect

Type Person struct {// 表 1 name string 'name' // 表 2 age int 'age' reflect.TypeOf(p4).Field(0).Tag ageTag := reflect.TypeOf(p4).Field(1).Tag fmt.Println(nameTag) fmt.Println(ageTag)Copy the code

Recursive nesting

Type Node struct {last *Node next *Node}Copy the code

Structure nesting (aggregation)

type Teacher struct { teacherName string TeacherNum int car Car } type Car struct { carBrand string carColor string } // TeacherName: "shaosiming", TeacherNum: = teacherName: "shaosiming", TeacherNum: TeacherName (" teacherName: ", teacherName) FMT.Println(" teacherName: ", teacherName) ", teacher.carbrand) FMT.Println(" teacher car color: ", teacher.carcolor)Copy the code

Anonymous fields

// s1 := Student{School{"Tsinghua", "wudaokou "}, "shaosiming", 18, 180} FMT.Println(s1) Can be dyed anonymous field names, FMT.Println(s1.schoolname) FMT.Println(s1.schoolname) FMT.Println(s1.schoolname) FMT.Println(s1.schoolname) FMT. FMT.Println(s1.addr) FMT.Println(s1.name) FMT.Println(s1.age) // If the anonymous field is not a compound type, it can be directly used. Anonymous field type name fmt.println (s1.float64)Copy the code

Field of the same name in an anonymous field

Type Owner struct {// When two anonymous fields have the same name, use: anonymous field type name. Dog Book} type Book struct {name string} type Dog struct {name string} owner := owner {Dog{"come blessing"} Book{"Three Body"}} FMT.Println(owner) // When a struct contains multiple anonymous struct fields and they have the same name, the name of the anonymous field type is called: Dog.name, Book.name fmt.Println(owner.Book.name) fmt.Println(owner.Dog.name)Copy the code

conclusion

Structure in Go: You can specify labels for fields in a structure. There are several ways to create it, either using {} or using new. Values in a structure can be initialized sequentially and values can be specified for a specified field. Can recursive nesting, can have anonymous fields, a variety of definitions and use, use on the love!!