add error handling

This commit is contained in:
Andrew Hurley 2022-11-12 14:28:47 +08:00
parent 7d7953c601
commit 2b9dc8fd19
1 changed files with 5 additions and 0 deletions

View File

@ -1,9 +1,14 @@
package greetings package greetings
import "fmt" import "fmt"
import "errors"
// 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