mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-19 03:31:16 +00:00
first
This commit is contained in:
86
course/4-structs/exercises/2-nested_structs/code.go
Normal file
86
course/4-structs/exercises/2-nested_structs/code.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type messageToSend struct {
|
||||
message string
|
||||
sender user
|
||||
recipient user
|
||||
}
|
||||
|
||||
type user struct {
|
||||
name string
|
||||
number int
|
||||
}
|
||||
|
||||
func canSendMessage(mToSend messageToSend) bool {
|
||||
// ?
|
||||
return true
|
||||
}
|
||||
|
||||
// don't touch below this line
|
||||
|
||||
func test(mToSend messageToSend) {
|
||||
fmt.Printf(`sending "%s" from %s (%v) to %s (%v)...`,
|
||||
mToSend.message,
|
||||
mToSend.sender.name,
|
||||
mToSend.sender.number,
|
||||
mToSend.recipient.name,
|
||||
mToSend.recipient.number,
|
||||
)
|
||||
fmt.Println()
|
||||
if canSendMessage(mToSend) {
|
||||
fmt.Println("...sent!")
|
||||
} else {
|
||||
fmt.Println("...can't send message")
|
||||
}
|
||||
fmt.Println("====================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(messageToSend{
|
||||
message: "you have an appointment tommorow",
|
||||
sender: user{
|
||||
name: "Brenda Halafax",
|
||||
number: 16545550987,
|
||||
},
|
||||
recipient: user{
|
||||
name: "Sally Sue",
|
||||
number: 19035558973,
|
||||
},
|
||||
})
|
||||
test(messageToSend{
|
||||
message: "you have an event tommorow",
|
||||
sender: user{
|
||||
number: 16545550987,
|
||||
},
|
||||
recipient: user{
|
||||
name: "Suzie Sall",
|
||||
number: 0,
|
||||
},
|
||||
})
|
||||
test(messageToSend{
|
||||
message: "you have an party tommorow",
|
||||
sender: user{
|
||||
name: "Njorn Halafax",
|
||||
number: 16545550987,
|
||||
},
|
||||
recipient: user{
|
||||
name: "Sally Sue",
|
||||
number: 19035558973,
|
||||
},
|
||||
})
|
||||
test(messageToSend{
|
||||
message: "you have a birthday tommorow",
|
||||
sender: user{
|
||||
name: "Eli Halafax",
|
||||
number: 0,
|
||||
},
|
||||
recipient: user{
|
||||
name: "Whitaker Sue",
|
||||
number: 19035558973,
|
||||
},
|
||||
})
|
||||
}
|
||||
97
course/4-structs/exercises/2-nested_structs/complete.go
Normal file
97
course/4-structs/exercises/2-nested_structs/complete.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type messageToSend struct {
|
||||
message string
|
||||
sender user
|
||||
recipient user
|
||||
}
|
||||
|
||||
type user struct {
|
||||
name string
|
||||
number int
|
||||
}
|
||||
|
||||
func canSendMessage(mToSend messageToSend) bool {
|
||||
if mToSend.recipient.number == 0 {
|
||||
return false
|
||||
}
|
||||
if mToSend.sender.number == 0 {
|
||||
return false
|
||||
}
|
||||
if mToSend.recipient.name == "" {
|
||||
return false
|
||||
}
|
||||
if mToSend.sender.name == "" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// don't touch below this line
|
||||
|
||||
func test(mToSend messageToSend) {
|
||||
fmt.Printf(`sending "%s" from %s (%v) to %s (%v)...`,
|
||||
mToSend.message,
|
||||
mToSend.sender.name,
|
||||
mToSend.sender.number,
|
||||
mToSend.recipient.name,
|
||||
mToSend.recipient.number,
|
||||
)
|
||||
fmt.Println()
|
||||
if canSendMessage(mToSend) {
|
||||
fmt.Println("...sent!")
|
||||
} else {
|
||||
fmt.Println("...can't send message")
|
||||
}
|
||||
fmt.Println("====================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(messageToSend{
|
||||
message: "you have an appointment tommorow",
|
||||
sender: user{
|
||||
name: "Brenda Halafax",
|
||||
number: 16545550987,
|
||||
},
|
||||
recipient: user{
|
||||
name: "Sally Sue",
|
||||
number: 19035558973,
|
||||
},
|
||||
})
|
||||
test(messageToSend{
|
||||
message: "you have an event tommorow",
|
||||
sender: user{
|
||||
number: 16545550987,
|
||||
},
|
||||
recipient: user{
|
||||
name: "Suzie Sall",
|
||||
number: 0,
|
||||
},
|
||||
})
|
||||
test(messageToSend{
|
||||
message: "you have an party tommorow",
|
||||
sender: user{
|
||||
name: "Njorn Halafax",
|
||||
number: 16545550987,
|
||||
},
|
||||
recipient: user{
|
||||
name: "Sally Sue",
|
||||
number: 19035558973,
|
||||
},
|
||||
})
|
||||
test(messageToSend{
|
||||
message: "you have a birthday tommorow",
|
||||
sender: user{
|
||||
name: "Eli Halafax",
|
||||
number: 0,
|
||||
},
|
||||
recipient: user{
|
||||
name: "Whitaker Sue",
|
||||
number: 19035558973,
|
||||
},
|
||||
})
|
||||
}
|
||||
12
course/4-structs/exercises/2-nested_structs/expected.txt
Normal file
12
course/4-structs/exercises/2-nested_structs/expected.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
sending "you have an appointment tommorow" from Brenda Halafax (16545550987) to Sally Sue (19035558973)...
|
||||
...sent!
|
||||
====================================
|
||||
sending "you have an event tommorow" from (16545550987) to Suzie Sall (0)...
|
||||
...can't send message
|
||||
====================================
|
||||
sending "you have an party tommorow" from Njorn Halafax (16545550987) to Sally Sue (19035558973)...
|
||||
...sent!
|
||||
====================================
|
||||
sending "you have a birthday tommorow" from Eli Halafax (0) to Whitaker Sue (19035558973)...
|
||||
...can't send message
|
||||
====================================
|
||||
34
course/4-structs/exercises/2-nested_structs/readme.md
Normal file
34
course/4-structs/exercises/2-nested_structs/readme.md
Normal file
@@ -0,0 +1,34 @@
|
||||
# Nested structs in Go
|
||||
|
||||
Structs can be nested to represent more complex entities:
|
||||
|
||||
```go
|
||||
type car struct {
|
||||
Make string
|
||||
Model string
|
||||
Height int
|
||||
Width int
|
||||
FrontWheel Wheel
|
||||
BackWheel Wheel
|
||||
}
|
||||
|
||||
type Wheel struct {
|
||||
Radius int
|
||||
Material string
|
||||
}
|
||||
```
|
||||
|
||||
The fields of a struct can be accessed using the dot `.` operator.
|
||||
|
||||
```go
|
||||
myCar := car{}
|
||||
myCar.FrontWheel.Radius = 5
|
||||
```
|
||||
|
||||
## Assignment
|
||||
|
||||
Textio has a bug, we've been sending texts with information missing! Before we send text messages in Textio, we should check to make sure the required fields have non-zero values.
|
||||
|
||||
Notice that the `user` struct is a nested struct within the `messageToSend` struct. Both `sender` and `recipient` are `user` struct types.
|
||||
|
||||
Complete the `canSendMessage` function. It should return `true` only if the `sender` and `recipient` fields each contain a `name` and a `number`. If any of the default zero values are present, return `false` instead.
|
||||
Reference in New Issue
Block a user