Add error handling

This commit is contained in:
Chris Cowley 2024-09-26 15:44:26 +02:00
parent 029676e88f
commit c709eaf71a

View file

@ -1,10 +1,18 @@
package greetings package greetings
import "fmt" import (
"errors"
"fmt"
)
// Hello returns a greeting for the named person. // Hello returns a greeting for the named person.
func Hello(name string) string { func Hello(name string) string {
// If no name was given, return an error with a message.
if name == "" {
return "", errors.New("empty name")
}
// Return a greeting that embeds the name in a message. // Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Welcome!", name) message := fmt.Sprintf("Hi, %v. Welcome!", name)
return message return message, nil
} }