Golang structure of the body structure and embedded anonymous member

Consider a two-dimensional drawing program, provide a variety of graphics library, such as rectangular, oval, and star-wheel-like geometry. Here is where the two definitions:

type Circle struct {
  X, Y, Radius int
}
type Wheel struct {
  X, Y, Radius, Spokes int
}

Representative of a circular type Circle contains the standard circle center X and Y coordinate information, and a Radius radius information represented. Wheel comprising a wheel-shaped in addition to all the members of all types Circle, but also increased the number of radial spokes Spokes represented. We can create a wheel variable:

 

var w Wheel
w.X = 8
w.Y = 8
w.Radius = 5
w.Spokes = 20

 

With the increase in the number of library geometry, we will notice similarities and duplication between them, so we might be the same for ease of maintenance of property independent:

ype Point struct {
  X, Y int
}
type Circle struct {
  Center Point
  Radius int
}
type Wheel struct {
  Circle Circle
  Spokes int
}

After this change the type of structure becomes clear, but this change also led to visit each member becomes cumbersome:

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

Go language has a feature that let's just declare a member of the corresponding data type without a named member name; called anonymous members of such members. Anonymous members of the data type must be named or refer to a type named type of pointer. The following code, Circle Wheel and each have an anonymous member. We can say that Point Circle type is embedded in the structure, while the Circle is embedded in the Wheel type structure.

 

type Circle struct {
  oint
  Radius int
}
type Wheel struct {
  Circle
  Spokes int
}

 

Revel in anonymity embedded feature, we can access the properties directly leaves without giving the full path:

var w Wheel
w.X = 8 // equivalent to w.Circle.Point.X = 8
w.Y = 8 // equivalent to w.Circle.Point.Y = 8
w.Radius = 5 // equivalent to w.Circle.Radius = 5
w.Spokes = 20

Syntax explicit form given in the comment on the right to access these leaf members still valid, and therefore are not really anonymous members inaccessible. In which the anonymous members of the Circle and Point has its own name - is named after the type name - but these names in the dot operator is optional. When we visit the child members may ignore any anonymous member section.

Unfortunately, the structure is not literal syntax brief representation anonymous members, so the following statement will not compile:

w = Wheel{8, 8, 5, 20} // compile error: unknown fields
w = Wheel{X: 8, Y: 8, Radius: 5, Spokes: 20} // compile error: unknown fields

Structure when the structure must follow the literal shape type declaration, so we can only use the following two syntaxes, are equivalent to each other:

w = Wheel{Circle{Point{8, 8}, 5}, 20}
w = Wheel{
    Circle: Circle{
        Point: Point{X:8, Y:8},
        Radius: 5,
    },
    Spokes: 20,
}

 

Guess you like

Origin www.cnblogs.com/xiangxiaolin/p/11902005.html