go-learning/hello/using_modules.go

36 lines
758 B
Go
Raw Normal View History

2025-08-30 12:55:18 +00:00
package using_modules
import (
"fmt"
"errors"
"math/rand"
)
// Hello returns a greeting for the named person
func Hello(name string) (string, error) {
// If no name was given, return an error with a message
if name == "" {
return "", errors.New("Empty name!")
}
message := fmt.Sprintf(randomFormat(), name)
return message, nil
}
// randomFormat returns one of a set of greeting messages. The returned
// message is selected at random.
func randomFormat() string {
// A slice of message formats
formats := []string{
"Hi, %v. Welcome!",
"Great to see you, %v",
"Hail, %v! Well met!",
}
// Return a randomly selected message format by specifying
// a random index for the slice of formats.
return formats[rand.Intn(len(formats))]
}