Definition of structure

A structure is a user-defined type that represents a collection of fields. Sometimes data should be integrated rather than disconnected. Structures can be used in this case.

Struct declaration

type Employee struct {
    FirstName string
    LastName  string
    Age       int
}
Copy the code

Coordinate the type and struct keywords of the structure. The type keyword of the Go language allows you to define various basic types as custom types. The names of structures cannot be repeated in the same package. If you want the structure to be accessible outside the package, capitalize the first letter of the structure. The field name of the structure must be unique. If the field is public, the first letter of the field name must also be capitalized.

Instantiation and initialization of a structure

Instantiation allocates specific memory in memory for the structure we create, and initializes fields in the instantiated structure with initial values.

instantiation

Instantiation allocates specific memory for storing fields to a structure that must be instantiated before it can be used. Instantiation creates a consistent memory region for the structure to store the structure based on its definition. Each instance of the structure is independent of each other.

Instantiate the structure directly as if you were declaring variables, and then assign values to the structure fields

Var e1 Employee e1.firstName = "wang" e1.lastname = "little five" e1.age = 29Copy the code

The new function allocates a memory area for the structure

E2 := new(Employee) // instantiate e2.FirstName = "zhang" e2.LastName = "three" e2.Age = 28Copy the code

Structures can also be addressed by &

E3 := &employee {} // Address to instantiate e3.FirstName = "zhang" e3.LastName = "little six" e3.Age = 27Copy the code

Initialize the

During the instantiation process, the structure’s fields can be initialized. The structure’s data can be assigned using jSON-like key-value pairs, and the initialization of the fields is optional.

package main import ( "fmt" ) type Employee struct { FirstName, Emp1 := Employee{FirstName: "", Age: 25, LastName: Println("Employee ", emp1) Println("Employee ", emp2)} emp2 := Employee{" Employee ", emp2)Copy the code

The output is as follows:

Employee 1 {Zhang Xiaohua 25} Employee 2 {Li Erniu 29}Copy the code

You can omit the keys of key-value pairs when all the fields of a structure need to be initialized and the fields are filled in the same order as they were defined in the structure body.

The field of a structure may also be a structure. Such structures are called nested structures.

package main import ( "fmt" ) type Address struct { city, County string} type Person struct {name string age int address address} func main() {var p Person p.name = "" P.age = 50 p.age = Address {city: "", county: } FMT.Println("Name:", p.name) fmt.Println("Age:",p.age) fmt.Println("City:",p.address.city) fmt.Println("County:",p.address.county) }Copy the code

If there are anonymous structure-type fields in a structure, the fields in that anonymous structure are called promoted fields. This is because the promotion field can be accessed directly by the external structure as if it belonged to it.

package main import ( "fmt" ) type Address struct { city, County string} type Person struct {name string age int Address} func main() {var p Person p.name = "" p.age = 35 P.adddress = Address{city: "", county: "YueChi} FMT. Println (" Name:", p.n ame) FMT. Println (" Age ", p.age) FMT. Println (" City ", Println("County:", p.county) // raise field}Copy the code

P.city and county, access the promotion fields city and county as if they were declared in the structure P. The program prints:

Name: ZhangQingshan Age: 35 City: Guang 'an County: YuechiCopy the code

methods

A method is simply a function with a special receiver type inserted between the func keyword and the method name. The receiver can be structural or non-structural. The sink is accessible inside the method.

func (t Type) methodName(parameter list) {
}
Copy the code

The code snippet above creates a method methodName with a receiver Type of Type. Compared to ordinary functions, there is one more receiver in the definition of a method. Each method can have only one receiver. The concept of receiver is similar to the object-oriented this or self, and the receiver attributes can be used directly in the method.

There are two types of receivers, pointer and non-pointer receivers. If the original member attributes need to be modified, the pointer type sink should be used.

Package main import "FMT" type Person struct {Name string Birth string ID int64 // ID ID} Func (person * person) changeName(name string) {person.name = name} Func (person person) printMessage() {fmt.printf ("My name is %v, and My birthday is %v, and My id is %v\n", Person.name, person.birth, person.id) // Try to modify personal information, Person.ID = 3} func main() {p1 := person {Name:" w ", Birth: "1992-12-22", ID:1, 1992-12-22, 1992-12-22, 1992-12-22, 1992-12-22, ID:1, 1992-12-22, 1992-12-22, ID:1,}Copy the code

The output is as follows:

My name is Wang Xiaoer, and My birthday is 1990-12-23, and My id is 1, and My birthday is 1990-12-23, and my id is 1Copy the code