From 77d19e959b03a5588da7b385f75ad52cd173d9a5 Mon Sep 17 00:00:00 2001 From: Amolith Date: Fri, 17 Sep 2021 15:42:13 -0400 Subject: [PATCH] tidy and add module dir --- hello/go.mod | 3 +++ modules/greetings/go.mod | 3 +++ modules/greetings/greetings.go | 39 ++++++++++++++++++++++++++++++++++ modules/hello/go.mod | 7 ++++++ modules/hello/hello.go | 28 ++++++++++++++++++++++++ go.mod => quote/go.mod | 2 +- go.sum => quote/go.sum | 0 7 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 hello/go.mod create mode 100644 modules/greetings/go.mod create mode 100644 modules/greetings/greetings.go create mode 100644 modules/hello/go.mod create mode 100644 modules/hello/hello.go rename go.mod => quote/go.mod (74%) rename go.sum => quote/go.sum (100%) diff --git a/hello/go.mod b/hello/go.mod new file mode 100644 index 0000000..76b2646 --- /dev/null +++ b/hello/go.mod @@ -0,0 +1,3 @@ +module example.com/hello + +go 1.17 diff --git a/modules/greetings/go.mod b/modules/greetings/go.mod new file mode 100644 index 0000000..efd7fb4 --- /dev/null +++ b/modules/greetings/go.mod @@ -0,0 +1,3 @@ +module example.com/greetings + +go 1.17 diff --git a/modules/greetings/greetings.go b/modules/greetings/greetings.go new file mode 100644 index 0000000..e1d294a --- /dev/null +++ b/modules/greetings/greetings.go @@ -0,0 +1,39 @@ +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 "", errors.New("empty name") + } + // Create a message using a random format. + message := fmt.Sprintf(randomFormat(), name) + return message, nil +} + +// init sets the 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 slide 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 fro the slice of formats. + return formats[rand.Intn(len(formats))] +} diff --git a/modules/hello/go.mod b/modules/hello/go.mod new file mode 100644 index 0000000..ca52192 --- /dev/null +++ b/modules/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/modules/hello/hello.go b/modules/hello/hello.go new file mode 100644 index 0000000..c092e9d --- /dev/null +++ b/modules/hello/hello.go @@ -0,0 +1,28 @@ +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 line, source file, and line number. + log.SetPrefix("greetings: ") + log.SetFlags(0) + + // Request a greeting message. + message, err := greetings.Hello("Gladys") + //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) +} diff --git a/go.mod b/quote/go.mod similarity index 74% rename from go.mod rename to quote/go.mod index 429a9b6..c855a4e 100644 --- a/go.mod +++ b/quote/go.mod @@ -1,4 +1,4 @@ -module git.nixnet.services/Amolith/messing-with-go +module example.com/quote go 1.17 diff --git a/go.sum b/quote/go.sum similarity index 100% rename from go.sum rename to quote/go.sum