learned I didn't need git for external modules and different folders can have different mains

This commit is contained in:
John O'Keefe 2024-02-03 11:00:34 -05:00
parent 943191e9dd
commit 19b1581f10
37 changed files with 439 additions and 16 deletions

View File

@ -1,3 +0,0 @@
module averageWithArgs
go 1.21.3

View File

@ -4,8 +4,8 @@ import (
"fmt"
"strings"
"git.linuxhg.com/Go_Training/datafile"
"git.linuxhg.com/Go_Training/keyboard"
"trying_out_go/pkg/datafile"
"trying_out_go/pkg/keyboard"
)
func averageHeadFirstSolution() {

View File

@ -8,7 +8,7 @@ import (
"strconv"
"strings"
"git.linuxhg.com/Go_Training/keyboard"
"trying_out_go/pkg/keyboard"
)
func averageMySolution() {

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"git.linuxhg.com/Go_Training/calendar"
"trying_out_go/pkg/calendar"
)
func companyCalendar() {

View File

@ -3,10 +3,10 @@ package main
import (
"fmt"
"git.linuxhg.com/Go_Training/datafile"
"git.linuxhg.com/Go_Training/keyboard"
"sort"
"strings"
"trying_out_go/pkg/datafile"
"trying_out_go/pkg/keyboard"
)
func countVotes() {

View File

@ -0,0 +1,28 @@
package main
import (
"fmt"
"os"
"path/filepath"
)
func scanDirectory(path string) {
fmt.Println(path)
files, err := os.ReadDir(path)
if err != nil {
panic(err)
}
for _, file := range files {
filePath := filepath.Join(path, file.Name())
if file.IsDir() {
scanDirectory(filePath)
} else {
fmt.Println(filePath)
}
}
}
func directory() {
scanDirectory("../../trying_out_go")
}

View File

@ -3,7 +3,7 @@ package main
import (
"fmt"
"git.linuxhg.com/Go_Training/magazine"
"trying_out_go/pkg/magazine"
)
func printInfo(s *magazine.Subscriber) {

View File

@ -4,7 +4,7 @@ import (
"fmt"
"log"
"git.linuxhg.com/Go_Training/keyboard"
"trying_out_go/pkg/keyboard"
)
func passFail() {

View File

@ -116,6 +116,12 @@ func main() {
companyCalendar()
},
},
{
name: "Directory List",
launchFunction: func() {
directory()
},
},
}
menu := wmenu.NewMenu("Choose a program.")
menu.Action(func(opts []wmenu.Opt) error {

View File

@ -4,7 +4,7 @@ import (
"fmt"
"log"
"git.linuxhg.com/Go_Training/keyboard"
"trying_out_go/pkg/keyboard"
)
func toCelsius() {

52
cmd/sum/sum.go Normal file
View File

@ -0,0 +1,52 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
func OpenFile(fileName string) (*os.File, error) {
fmt.Println("Opening", fileName)
return os.Open(fileName)
}
func CloseFile(file *os.File) {
fmt.Println("Closing file")
file.Close()
}
func GetFloats(fileName string) ([]float64, error) {
var numbers []float64
file, err := OpenFile(fileName)
if err != nil {
return nil, err
}
defer CloseFile(file)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
number, err := strconv.ParseFloat(scanner.Text(), 64)
if err != nil {
return nil, err
}
numbers = append(numbers, number)
}
if scanner.Err() != nil {
return nil, scanner.Err()
}
return numbers, nil
}
func main() {
numbers, err := GetFloats(os.Args[1])
if err != nil {
log.Fatal(err)
}
var sum float64 = 0
for _, number := range numbers {
sum += number
}
fmt.Printf("Sum: %0.2f\n", sum)
}

View File

@ -3,9 +3,6 @@ module trying_out_go
go 1.21.5
require (
git.linuxhg.com/Go_Training/datafile v0.0.0-20240111160218-6989e96515a9
git.linuxhg.com/Go_Training/keyboard v0.0.0-20240111160241-d208f095efce
git.linuxhg.com/Go_Training/magazine v0.0.0-20240112152452-7bd91fa7e6c2
github.com/charmbracelet/bubbles v0.17.1
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/lipgloss v0.9.1
@ -13,7 +10,6 @@ require (
)
require (
git.linuxhg.com/Go_Training/calendar v0.0.0-20240131153240-372e0ebc267d // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect

56
pkg/calendar/date.go Normal file
View File

@ -0,0 +1,56 @@
package calendar
import "errors"
// Date Struct
type Date struct {
year int
month int
day int
}
// Setter Methods
// SetYear Method
func (d *Date) SetYear(year int) error {
if year < 1 {
return errors.New("invalid year")
}
d.year = year
return nil
}
// SetMonth Method
func (d *Date) SetMonth(month int) error {
if month < 1 || month > 12 {
return errors.New("invalid month")
}
d.month = month
return nil
}
// SetDay Method
func (d *Date) SetDay(day int) error {
if day < 1 || day > 31 {
return errors.New("invalid day")
}
d.day = day
return nil
}
// Getter Methods
// Year Method
func (d *Date) Year() int {
return d.year
}
// Month Method
func (d *Date) Month() int {
return d.month
}
// Day Method
func (d *Date) Day() int {
return d.day
}

23
pkg/calendar/event.go Normal file
View File

@ -0,0 +1,23 @@
package calendar
import (
"errors"
"unicode/utf8"
)
type Event struct {
title string
Date
}
func (e *Event) Title() string {
return e.title
}
func (e *Event) SetTitle(title string) error {
if utf8.RuneCountInString(title) > 32 {
return errors.New("invalid title length")
}
e.title = title
return nil
}

32
pkg/datafile/floats.go Normal file
View File

@ -0,0 +1,32 @@
package datafile
import (
"bufio"
"os"
"strconv"
)
// GetFloats reads a float64 from each line of a file.
func GetFloats(fileName string) ([]float64, error) {
var numbers []float64
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
number, err := strconv.ParseFloat(scanner.Text(), 64)
if err != nil {
return nil, err
}
numbers = append(numbers, number)
}
err = file.Close()
if err != nil {
return nil, err
}
if scanner.Err() != nil {
return numbers, scanner.Err()
}
return numbers, nil
}

31
pkg/datafile/strings.go Normal file
View File

@ -0,0 +1,31 @@
package datafile
import (
"bufio"
"os"
)
func GetStrings(filename string) ([]string, error) {
var lines []string
file, err := os.Open(filename)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
}
err = file.Close()
if err != nil {
return nil, err
}
if scanner.Err() != nil {
return nil, scanner.Err()
}
return lines, nil
}

56
pkg/date/date.go Normal file
View File

@ -0,0 +1,56 @@
package calendar
import "errors"
// Date Struct
type Date struct {
year int
month int
day int
}
// Setter Methods
// SetYear Method
func (d *Date) SetYear(year int) error {
if year < 1 {
return errors.New("invalid year")
}
d.year = year
return nil
}
// SetMonth Method
func (d *Date) SetMonth(month int) error {
if month < 1 || month > 12 {
return errors.New("invalid month")
}
d.month = month
return nil
}
// SetDay Method
func (d *Date) SetDay(day int) error {
if day < 1 || day > 31 {
return errors.New("invalid day")
}
d.day = day
return nil
}
// Getter Methods
// Year Method
func (d *Date) Year() int {
return d.year
}
// Month Method
func (d *Date) Month() int {
return d.month
}
// Day Method
func (d *Date) Day() int {
return d.day
}

23
pkg/date/event.go Normal file
View File

@ -0,0 +1,23 @@
package calendar
import (
"errors"
"unicode/utf8"
)
type Event struct {
title string
Date
}
func (e *Event) Title() string {
return e.title
}
func (e *Event) SetTitle(title string) error {
if utf8.RuneCountInString(title) > 32 {
return errors.New("invalid title length")
}
e.title = title
return nil
}

33
pkg/gadget/program.go Normal file
View File

@ -0,0 +1,33 @@
package main
type Player interface {
Play(string)
Stop()
}
func TryOut(player Player) {
player.Play("Test Track")
player.Stop()
recorder, ok := player.(TapeRecorder)
if ok {
recorder.Record()
}
}
func playList(device Player, songs []string) {
for _, song := range songs {
device.Play(song)
}
device.Stop()
}
func main() {
mixTape := []string{"Jessie's Girl", "Whip It", "9 to 5"}
var player Player = TapePlayer{}
playList(player, mixTape)
player = TapeRecorder{}
playList(player, mixTape)
TryOut(TapeRecorder{})
TryOut(TapePlayer{})
}

31
pkg/gadget/tape.go Normal file
View File

@ -0,0 +1,31 @@
package main
import "fmt"
type TapePlayer struct {
Batteries string
}
func (t TapePlayer) Play(song string) {
fmt.Println("Playing", song)
}
func (t TapePlayer) Stop() {
fmt.Println("Stopped")
}
type TapeRecorder struct {
Microphone int
}
func (t TapeRecorder) Play(song string) {
fmt.Println("Playing", song)
}
func (t TapeRecorder) Record() {
fmt.Println("Recording")
}
func (t TapeRecorder) Stop() {
fmt.Println("Stopped")
}

38
pkg/keyboard/keyboard.go Normal file
View File

@ -0,0 +1,38 @@
// Package keyboard reads user input from the keyboard.
package keyboard
import (
"bufio"
"os"
"strconv"
"strings"
)
// GetString reads a string from the keyboard.
// It returns the string read and any error encountered.
func GetString() (string, error) {
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return "", err
}
return input, nil
}
// GetFloat reads a floating-point number from the keyboard.
// It returns the number read and any error encountered.
func GetFloat() (float64, error) {
input, err := GetString()
if err != nil {
return 0, err
}
input = strings.TrimSpace(input)
number, err := strconv.ParseFloat(input, 64)
if err != nil {
return 0, err
}
return number, nil
}

21
pkg/magazine/types.go Normal file
View File

@ -0,0 +1,21 @@
package magazine
type Subscriber struct {
Name string
Rate float64
Active bool
Address
}
type Employee struct {
Name string
Salary float64
Address
}
type Address struct {
Street string
City string
State string
PostalCode string
}