tidy and add module dir

This commit is contained in:
Amolith 2021-09-17 15:42:13 -04:00
parent de4704527e
commit 77d19e959b
Signed by: Amolith
GPG Key ID: 5548AD9930655715
7 changed files with 81 additions and 1 deletions

3
hello/go.mod Normal file
View File

@ -0,0 +1,3 @@
module example.com/hello
go 1.17

3
modules/greetings/go.mod Normal file
View File

@ -0,0 +1,3 @@
module example.com/greetings
go 1.17

View File

@ -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))]
}

7
modules/hello/go.mod Normal file
View File

@ -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

28
modules/hello/hello.go Normal file
View File

@ -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)
}

View File

@ -1,4 +1,4 @@
module git.nixnet.services/Amolith/messing-with-go
module example.com/quote
go 1.17