# Type Embedding

In order to achieve composition, we need to take a look at type embedding in Go. Suppose we are trying to build a car. A car is composed of many small moving parts. We want to be able to reuse these small moving parts to build different type of cars.

## Struct Embedding

First we have a turbo charged four cylinder engine.

```go
type TurboCharged struct {
  Horsepower   int
  Torque       int
  ChargerCount int
  FanSpeed     float64
}
```

We can define couple methods on the engine.

```go
func (tc TurboCharged) ignite() {
  fmt.Printf("4 cylinder is running, %d chargers are ready\n", tc.ChargerCount)
}
```

Now let's build a car with this turbo charged engine by embedding it into a car struct.

```go
type Car struct {
  TurboCharged
}

func (c Car) Start() {
  c.ignite()
}

func main() {
  c := Car{TurboCharged{300, 350, 2, 100.5}}
  c.Start()
}
```

Output:

```
4 cylinder is running, 2 chargers are ready
```

Notice that `ignite` is also embedded onto `Car`. I made it private so that starting an engine is abstracted away from user.

## Interface Embedding

Now you may ask, what if I want a different engine like a naturally aspirated 6 cylinder? You can achieve it by embedding interfaces.

```go
type Engine interface {
  ignite()
}

type NatAspirated struct {
  Horsepower int
  Torque     int
}

func (na NatAspirated) ignite() {
  fmt.Printf("6 cylinder is running, VROOOOMMMMM\n")
}
```

Change the embedded type to `Engine`

```go
type Car struct {
  Engine
}

func (c Car) Start() {
  c.ignite()
}
```

Now we can put whatever engine we like into our car.

```go
func main() {
  var c Car

  c = Car{NatAspirated{400, 450}}
  // OR
  c = Car{TurboCharged{300, 305, 2, 100.5}}

  c.Start()
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://calvinfeng.gitbook.io/gonotebook/idioms/type-embedding.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
