Files
bootdotdev-fcc-learn-golang…/course/4-structs/exercises/4-embedded_structs/readme.md
wagslane 9be3074de6 first
2023-05-01 15:25:27 -06:00

47 lines
1.4 KiB
Markdown

# Embedded Structs
Go is not an [object-oriented](https://en.wikipedia.org/wiki/Object-oriented_programming) language. However, embedded structs provide a kind of *data-only* inheritance that can be useful at times. Keep in mind, Go doesn't support classes or inheritance in the complete sense, embedded structs are just a way to elevate and share fields between struct definitions.
```go
type car struct {
make string
model string
}
type truck struct {
// "car" is embedded, so the definition of a
// "truck" now also additionally contains all
// of the fields of the car struct
car
bedSize int
}
```
## Embedded vs nested
* An embedded struct's fields are accessed at the top level, unlike nested structs.
* Promoted fields can be accessed like normal fields except that they can't be used in [composite literals](https://golang.org/ref/spec#Composite_literals)
```go
lanesTruck := truck{
bedSize: 10,
car: car{
make: "toyota",
model: "camry",
},
}
fmt.Println(lanesTruck.bedSize)
// embedded fields promoted to the top-level
// instead of lanesTruck.car.make
fmt.Println(lanesTruck.make)
fmt.Println(lanesTruck.model)
```
## Assignment
At Textio, a "user" struct represents an account holder, and a "sender" is just a "user" with some "sender" specific data. A "sender" is a user that has a `rateLimit` field that tells us how many messages they are allowed to send.
Fix the system by using an embedded struct as expected by the test code.