This is the 15th day of my participation in the August More Text Challenge

The structure of the body

Structural foundation

Structs are aggregate data types that combine zero or more named variables of any type. Each variable is called a member of the structure

Here we define a structure called Employee and a structure variable called dilbert

type Employee struct {
    ID int
    Name string
    Address string
    Salary int
    Position string
}
var dilbert Employee
Copy the code

Each member of Dilbert is accessed by a dot, such as dilbert.name. Because Dilbert is a variable, all of its members are variables, so you can assign values to members of a structure

// Get the address of the member variable, Position *position = "Senior "+ *position // The dot can also be used on the structure pointer var employeeOfTheMonth *Employee = &dilbert employeeOfTheMonth.Position += "(proactive team player)"Copy the code

There is no need to prefix employeeOfTheMonth with an asterisk (*) as in C++

A structure’s member variables are usually written one line at a time, with the variable Name preceding the type, but consecutive member variables of the same type can be written on a single line, such as Name and Address below

type Employee struct {
    ID int
    Name,Address string
    Salary int
    Position string
}
Copy the code

The order of member variables is important for structure identity. If we combine Position, which is also a string, with Name and Address, or swap the order of Name and Address, we define a different struct type

A structure cannot define a member variable that has the same structure type as itself, that is, an aggregate type cannot contain itself (the same is true for arrays). But a structure can contain Pointers of its own type, as follows:

type Node struct {
    Value int
    Left, Right *Node
}
Copy the code

The zero value of a structure consists of the zero value of the members of the structure. A structure without any member variables is called an empty structure and is written struct{}. It has no length and carries no information

Structure literals

The value of a structure can be set by a structure literal

type Point struct {
    X, Y int
}
p := Point{1, 2}
Copy the code

This kind of initialization is unfriendly and adds a burden to the developer and reader who must remember the order of each member variable, as well as to the future expansion or rearrangement of the structure’s member variables. So a different approach is recommended

p := Point{X:1, Y:2}
Copy the code

If a member variable is not specified in this initialization, its value is the zero value of the member variable type

The value of a struct type can be passed to a function as an argument or as a return value of the function

Func Scale(p Point, factor int) Point{return Point{p.X * factor, p.Y * factor}} FMT.Println(Scale(Point{1,2}, 5)) / / "{5, 10}"Copy the code

For efficiency reasons, large structures are usually passed directly to or returned from a function using a structure pointer (this is also necessary when the function needs to modify the structure’s contents).

func Bonus(e *Employee, percent int) int {
    return e.Salary * percent/100
}
Copy the code

Structural comparison

A structure is comparable if all of its member variables are comparable

type Point struct {
    X, Y int
}
p := Point{1, 2}
q := Point{2, 1}
fmt.Println(p.X == q.X && p.Y == q.Y) // false
fmt.Println(p == q) // false
Copy the code

A comparable structure type, like any other type, can be used as a map key

type address struct {
    hostname string
    port int
}
hits := make(map[address]int)
hits[address{"golang.org", 443}]++
Copy the code

Structure nesting

Suppose the following structures are now defined

type Point struct {
    X, Y int
}
type Circle struct {
    Center Point
    Radius int
}
type Wheel struct {
    Circle Circle
    Spokes int
}
Copy the code

The program looks clean, but accessing the Wheel members becomes cumbersome

var w Wheel
w.Circle.Center.X = 8
w.Circle.Center.Y = 8
w.Circle.Radius = 5
w.Spokes = 20
Copy the code

Go allows us to define structure members without names by specifying their types. The type of the structure member must be a named type or a pointer to a named type

Type Circle struct {Point Radius int} type Wheel struct {Circle Spokes int} X = 8 w.X = 8 // is equivalent to w.circle.center.Y = 8 w.radius = 5 // Equivalent to w.circle. Radius = 5 w.spokes = 20Copy the code

Structure literals have no quick way to initialize nested structures, so the syntax below is wrong

W = Wheel,8,5,20 {8} / / compile error w = Wheel {X: 8, Y: 8, the Radius: 5, Spokes: 20} / / compile errors / / need such w = Wheel {Circle: a Circle {Point: Point{X:8,Y:8}, Radius: 5,}, Spokes: 20,}Copy the code