Browse Source

tidy and add module dir

master
Amolith 3 months ago
parent
commit
77d19e959b
Signed by: Amolith GPG Key ID: 5548AD9930655715
  1. 3
      hello/go.mod
  2. 3
      modules/greetings/go.mod
  3. 39
      modules/greetings/greetings.go
  4. 7
      modules/hello/go.mod
  5. 28
      modules/hello/hello.go
  6. 2
      quote/go.mod
  7. 0
      quote/go.sum

3
hello/go.mod

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

3
modules/greetings/go.mod

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

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

7
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

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

2
go.mod → quote/go.mod

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

0
go.sum → quote/go.sum

Loading…
Cancel
Save