Initial commit - learning the basics
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
module learning.local/hello
|
||||
|
||||
go 1.24.5
|
||||
|
||||
replace learning.local/using_modules => ../using_modules
|
||||
|
||||
require learning.local/using_modules v0.0.0-00010101000000-000000000000
|
||||
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"learning.local/using_modules"
|
||||
)
|
||||
|
||||
func main () {
|
||||
// Set properties of the predefined Logger, including
|
||||
// the log entry prefix and a flag to disable printing
|
||||
// the time, source fiile, and line number.
|
||||
log.SetPrefix("using_modules: ")
|
||||
log.SetFlags(0)
|
||||
|
||||
// A slice of names
|
||||
names := []string{"Gladys", "Yoda", "Darth Vader"}
|
||||
|
||||
// Request greeting messages for the names
|
||||
messages, err := using_modules.Hellos(names)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// If no error was returned, print the returned map of
|
||||
// messages to the console
|
||||
fmt.Println(messages)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package using_modules
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"errors"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
// 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!")
|
||||
}
|
||||
|
||||
message := fmt.Sprintf(randomFormat(), name)
|
||||
return message, nil
|
||||
}
|
||||
|
||||
|
||||
// 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))]
|
||||
}
|
||||
Reference in New Issue
Block a user