Initial commit - learning the basics

This commit is contained in:
V
2025-08-30 13:55:18 +01:00
commit 194ca9b7a9
27 changed files with 830 additions and 0 deletions
+7
View File
@@ -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
+28
View File
@@ -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)
}
+35
View File
@@ -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))]
}