Files
bootdotdev-fcc-learn-golang…/course/10-advanced_functions/exercises/3-currying/complete.go
wagslane 9be3074de6 first
2023-05-01 15:25:27 -06:00

50 lines
1.2 KiB
Go

package main
import (
"errors"
"fmt"
)
// getLogger takes a function that formats two strings into
// a single string and returns a function that formats two strings but prints
// the result instead of returning it
func getLogger(formatter func(string, string) string) func(string, string) {
return func(first, second string) {
fmt.Println(formatter(first, second))
}
}
// don't touch below this line
func test(first string, errors []error, formatter func(string, string) string) {
defer fmt.Println("====================================")
logger := getLogger(formatter)
fmt.Println("Logs:")
for _, err := range errors {
logger(first, err.Error())
}
}
func colonDelimit(first, second string) string {
return first + ": " + second
}
func commaDelimit(first, second string) string {
return first + ", " + second
}
func main() {
dbErrors := []error{
errors.New("out of memory"),
errors.New("cpu is pegged"),
errors.New("networking issue"),
errors.New("invalid syntax"),
}
test("Error on database server", dbErrors, colonDelimit)
mailErrors := []error{
errors.New("email too large"),
errors.New("non alphanumeric symbols found"),
}
test("Error on mail server", mailErrors, commaDelimit)
}