Add error handling
This commit is contained in:
parent
029676e88f
commit
c709eaf71a
1 changed files with 10 additions and 2 deletions
12
greetings.go
12
greetings.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue