This is the 17th day of my participation in Gwen Challenge

These reviews

Previous articles have focused on constructs and methods in the Go language. A structure is a collection of data with the same or different types of data.

Following the introduction of structures, this article will introduce the use of embedding and composition in the Go language.

Embedding and composition

When defining a structure, Golang allows you to declare a field without a field name. This form of field is called type-embedded or anonymous. In this case, the field name is the field type itself. See the following example:

type temp struct{
	string
	int
}
Copy the code

If the nested type is a structure, all members of the nested structure can be accessed directly, as shown in the following code:

package main

import "fmt"

type Wheel struct {
	shape string
}

type Car struct {
	Wheel
	Name string
}

func main(a)  {

	car := &Car {
		Wheel {
			"Round",},"Ford",
	}
	fmt.Println(car.Name, car.shape, "") // The ford is round
}

Copy the code

In the code above, we embed the Wheel body into Car so that member attributes in the Wheel body can be accessed directly through references to Car.

Structure embedding can vividly simulate the characteristics of composition in object-oriented language design. Structure embedding is a combination property. Complex structures with different basic properties can be constructed by combining different basic structures. Let’s take a look at the following example.

Ducks can both fly and swim, but not all ducks can do both. We first define these two behaviors by structuring them as follows:

// Swimming features
type Swimming struct{}func (swim *Swimming) swim(a)  {
	fmt.Println("swimming is my ability")}// Flight characteristics
type Flying struct{}func (fly *Flying) fly(a)  {
	fmt.Println("flying is my ability")}Copy the code

The wild duck can fly and swim. A domestic duck can only swim. We can embed different structures between them to reflect different behaviors:

// WildDuck type WildDuck struct {Swimming Flying} // WildDuck type WildDuck struct {Swimming}Copy the code

Finally, check whether they have corresponding behavior characteristics in the main function:

Func main() {wild := WildDuck{} wild. Fly () wild. Swim () Domestic := DomesticDuck{} domestic.swim()}Copy the code

As can be seen from the above examples, Golang’s embedded structure features can realize the combination of objects, add various functions and features to the structure, and improve the reusability and extensibility of the code.

summary

This paper mainly introduces the embedded and combined properties of the structure. In order to achieve the composition feature of objects in the Go language, embedding and composition can be used to add various functions to structures.

At this point, the basic syntax of Go is covered. Go is a very dynamic new generation of languages. It is designed to serve concurrency, and it combines the advantages of many excellent programming languages today, such as automatic garbage collection, fast static type checking, and high performance. In this series of articles, we have introduced the basic syntax of Go and some of the language’s features, including native data types, containers, functions, constructs, etc., to give you a basic understanding of the syntax of Go. Through the study of this series of articles, we have the basic Go development ability.

In the next series of articles, I’ll take a closer look at Go and introduce advanced features like package management, reflection, and concurrency.

Read the latest article, pay attention to the public number: AOHO Qiusuo