commit 4a3f455d061c343b1c65bca7fccf4d22ba252ee9 Author: GO Date: Thu Sep 2 19:08:57 2021 +0000 first commit diff --git a/createAGoModule/greetings/go.mod b/createAGoModule/greetings/go.mod new file mode 100644 index 0000000..efd7fb4 --- /dev/null +++ b/createAGoModule/greetings/go.mod @@ -0,0 +1,3 @@ +module example.com/greetings + +go 1.17 diff --git a/createAGoModule/greetings/greetings.go b/createAGoModule/greetings/greetings.go new file mode 100644 index 0000000..1b1736c --- /dev/null +++ b/createAGoModule/greetings/greetings.go @@ -0,0 +1,59 @@ +package greetings + +import ( + "errors" + "fmt" + "math/rand" + "time" +) + +// 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 name, errors.New("empty name") + } + // Create a message using a random format. + message := fmt.Sprintf(randomFormat(), name) +// message := fmt.Sprintf(randomFormat()) + return message, nil +} + +// Hellos returns a map that associates each of the named people +// with a greeting message. +func Hellos(names []string) (map[string]string, error) { + // A map to associate names with messages. + messages := make(map[string]string) + // Loop through the received slice of names, calling + // the Hello function to get a message for each name. + for _, name := range names { + message, err := Hello(name) + if err != nil { + return nil, err + } + // In the map, associate the retrieved message with + // the name. + messages[name] = message + } + return messages, nil +} + +// init sets initial values for variables used in the function. +func init() { + rand.Seed(time.Now().UnixNano()) +} + +// 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))] +} \ No newline at end of file diff --git a/createAGoModule/greetings/greetings_test.go b/createAGoModule/greetings/greetings_test.go new file mode 100644 index 0000000..8754141 --- /dev/null +++ b/createAGoModule/greetings/greetings_test.go @@ -0,0 +1,26 @@ +package greetings + +import ( + "testing" + "regexp" +) + +// TestHelloName calls greetings.Hello with a name, checking +// for a valid return value. +func TestHelloName(t *testing.T) { + name := "Gladys" + want := regexp.MustCompile(`\b`+name+`\b`) + msg, err := Hello("Gladys") + if !want.MatchString(msg) || err != nil { + t.Fatalf(`Hello("Gladys") = %q, %v, want match for %#q, nil`, msg, err, want) + } +} + +// TestHelloEmpty calls greetings.Hello with an empty string, +// checking for an error. +func TestHelloEmpty(t *testing.T) { + msg, err := Hello("") + if msg != "" || err == nil { + t.Fatalf(`Hello("") = %q, %v, want "", error`, msg, err) + } +} \ No newline at end of file diff --git a/createAGoModule/hello/go.mod b/createAGoModule/hello/go.mod new file mode 100644 index 0000000..ca52192 --- /dev/null +++ b/createAGoModule/hello/go.mod @@ -0,0 +1,7 @@ +module example.com/hello + +go 1.17 + +replace example.com/greetings => ../greetings + +require example.com/greetings v0.0.0-00010101000000-000000000000 diff --git a/createAGoModule/hello/hello b/createAGoModule/hello/hello new file mode 100755 index 0000000..c077c79 Binary files /dev/null and b/createAGoModule/hello/hello differ diff --git a/createAGoModule/hello/hello.go b/createAGoModule/hello/hello.go new file mode 100644 index 0000000..58d5527 --- /dev/null +++ b/createAGoModule/hello/hello.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "log" + + "example.com/greetings" +) + +func main() { + // Set properties of the predefined Logger, including + // the log entry prefix and a flag to disable printing + // the time, source file, and line number. + log.SetPrefix("greetings: ") + log.SetFlags(0) + + + // Request a greeting message. + // message, err := greetings.Hello("Gladys") + + // A slice of names. + names := []string{"Gladys", "Samantha", "Darrin"} + message, err := greetings.Hellos(names) + + // If an error was returned, print it to the console and + // exit the program. + if err != nil { + log.Fatal(err) + } + + // If no error was returned, print the returned message + // to the console. + fmt.Println(message) +} \ No newline at end of file