mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-17 02:31:17 +00:00
first
This commit is contained in:
46
course/4-structs/exercises/4-embedded_structs/readme.md
Normal file
46
course/4-structs/exercises/4-embedded_structs/readme.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# 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.
|
||||
Reference in New Issue
Block a user