From 2b9dc8fd192d993d5c4ea14e429d1639a1c3454c Mon Sep 17 00:00:00 2001 From: Andrew Hurley Date: Sat, 12 Nov 2022 14:28:47 +0800 Subject: [PATCH] add error handling --- greetings.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/greetings.go b/greetings.go index ab4dfa9..e56a36f 100644 --- a/greetings.go +++ b/greetings.go @@ -1,9 +1,14 @@ package greetings import "fmt" +import "errors" // Hello returns a greeting for the named person. 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. message := fmt.Sprintf("Hi, %v. Welcome!", name) return message