tidy and add module dir
This commit is contained in:
parent
de4704527e
commit
77d19e959b
|
@ -0,0 +1,3 @@
|
|||
module example.com/hello
|
||||
|
||||
go 1.17
|
|
@ -0,0 +1,3 @@
|
|||
module example.com/greetings
|
||||
|
||||
go 1.17
|
|
@ -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))]
|
||||
}
|
|
@ -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
|
|
@ -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)
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
module git.nixnet.services/Amolith/messing-with-go
|
||||
module example.com/quote
|
||||
|
||||
go 1.17
|
||||
|
Loading…
Reference in New Issue