mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-18 19:21:17 +00:00
first
This commit is contained in:
60
course/6-errors/exercises/1-errors/complete.go
Normal file
60
course/6-errors/exercises/1-errors/complete.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func sendSMSToCouple(msgToCustomer, msgToSpouse string) (float64, error) {
|
||||
cost, err := sendSMS(msgToCustomer)
|
||||
if err != nil {
|
||||
return 0.0, err
|
||||
}
|
||||
|
||||
costSpouse, err := sendSMS(msgToSpouse)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return costSpouse + cost, nil
|
||||
}
|
||||
|
||||
// don't edit below this line
|
||||
|
||||
func sendSMS(message string) (float64, error) {
|
||||
const maxTextLen = 25
|
||||
const costPerChar = .0002
|
||||
if len(message) > maxTextLen {
|
||||
return 0.0, fmt.Errorf("can't send texts over %v characters", maxTextLen)
|
||||
}
|
||||
return costPerChar * float64(len(message)), nil
|
||||
}
|
||||
|
||||
func test(msgToCustomer, msgToSpouse string) {
|
||||
defer fmt.Println("========")
|
||||
fmt.Println("Message for customer:", msgToCustomer)
|
||||
fmt.Println("Message for spouse:", msgToSpouse)
|
||||
totalCost, err := sendSMSToCouple(msgToCustomer, msgToSpouse)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Total cost: $%.4f\n", totalCost)
|
||||
}
|
||||
|
||||
func main() {
|
||||
test(
|
||||
"Thanks for coming in to our flower shop today!",
|
||||
"We hope you enjoyed your gift.",
|
||||
)
|
||||
test(
|
||||
"Thanks for joining us!",
|
||||
"Have a good day.",
|
||||
)
|
||||
test(
|
||||
"Thank you.",
|
||||
"Enjoy!",
|
||||
)
|
||||
test(
|
||||
"We loved having you in!",
|
||||
"We hope the rest of your evening is absolutely fantastic.",
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user