From 19b1581f10c0fa2944c459d835c01c2502cb876d Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Sat, 3 Feb 2024 11:00:34 -0500 Subject: [PATCH] learned I didn't need git for external modules and different folders can have different mains --- averageWithArgs/go.mod | 3 - .../averageWithArgs}/averageWithArgs.go | 0 .../headFirstCodeMagnets}/variadic.go | 0 .../playground}/averageHeadFirstSolution.go | 4 +- .../playground}/averageHeadFirstVariadic.go | 0 .../playground}/averageMySolution.go | 2 +- .../playground}/averageVariadic.go | 0 {playground => cmd/playground}/calendar.go | 2 +- {playground => cmd/playground}/countVotes.go | 4 +- cmd/playground/directory.go | 28 ++++++++++ {playground => cmd/playground}/double.go | 0 {playground => cmd/playground}/error.go | 0 {playground => cmd/playground}/fuel.go | 0 {playground => cmd/playground}/guess.go | 0 .../playground}/magazineSubscribers.go | 2 +- .../playground}/myIntPointer.go | 0 {playground => cmd/playground}/passfail.go | 2 +- {playground => cmd/playground}/playground.go | 6 ++ {playground => cmd/playground}/shopping.go | 0 {playground => cmd/playground}/slices.go | 0 {playground => cmd/playground}/test.go | 0 {playground => cmd/playground}/toCelsius.go | 2 +- {playground => cmd/playground}/variadic.go | 0 {playground => cmd/playground}/wallArea.go | 0 cmd/sum/sum.go | 52 +++++++++++++++++ playground/go.mod => go.mod | 4 -- playground/go.sum => go.sum | 0 pkg/calendar/date.go | 56 +++++++++++++++++++ pkg/calendar/event.go | 23 ++++++++ pkg/datafile/floats.go | 32 +++++++++++ pkg/datafile/strings.go | 31 ++++++++++ pkg/date/date.go | 56 +++++++++++++++++++ pkg/date/event.go | 23 ++++++++ pkg/gadget/program.go | 33 +++++++++++ pkg/gadget/tape.go | 31 ++++++++++ pkg/keyboard/keyboard.go | 38 +++++++++++++ pkg/magazine/types.go | 21 +++++++ 37 files changed, 439 insertions(+), 16 deletions(-) delete mode 100644 averageWithArgs/go.mod rename {averageWithArgs => cmd/averageWithArgs}/averageWithArgs.go (100%) rename {headFirstCodeMagnets => cmd/headFirstCodeMagnets}/variadic.go (100%) rename {playground => cmd/playground}/averageHeadFirstSolution.go (84%) rename {playground => cmd/playground}/averageHeadFirstVariadic.go (100%) rename {playground => cmd/playground}/averageMySolution.go (94%) rename {playground => cmd/playground}/averageVariadic.go (100%) rename {playground => cmd/playground}/calendar.go (90%) rename {playground => cmd/playground}/countVotes.go (92%) create mode 100644 cmd/playground/directory.go rename {playground => cmd/playground}/double.go (100%) rename {playground => cmd/playground}/error.go (100%) rename {playground => cmd/playground}/fuel.go (100%) rename {playground => cmd/playground}/guess.go (100%) rename {playground => cmd/playground}/magazineSubscribers.go (95%) rename {playground => cmd/playground}/myIntPointer.go (100%) rename {playground => cmd/playground}/passfail.go (88%) rename {playground => cmd/playground}/playground.go (96%) rename {playground => cmd/playground}/shopping.go (100%) rename {playground => cmd/playground}/slices.go (100%) rename {playground => cmd/playground}/test.go (100%) rename {playground => cmd/playground}/toCelsius.go (87%) rename {playground => cmd/playground}/variadic.go (100%) rename {playground => cmd/playground}/wallArea.go (100%) create mode 100644 cmd/sum/sum.go rename playground/go.mod => go.mod (79%) rename playground/go.sum => go.sum (100%) create mode 100644 pkg/calendar/date.go create mode 100644 pkg/calendar/event.go create mode 100644 pkg/datafile/floats.go create mode 100644 pkg/datafile/strings.go create mode 100644 pkg/date/date.go create mode 100644 pkg/date/event.go create mode 100644 pkg/gadget/program.go create mode 100644 pkg/gadget/tape.go create mode 100644 pkg/keyboard/keyboard.go create mode 100644 pkg/magazine/types.go diff --git a/averageWithArgs/go.mod b/averageWithArgs/go.mod deleted file mode 100644 index aabddcc..0000000 --- a/averageWithArgs/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module averageWithArgs - -go 1.21.3 diff --git a/averageWithArgs/averageWithArgs.go b/cmd/averageWithArgs/averageWithArgs.go similarity index 100% rename from averageWithArgs/averageWithArgs.go rename to cmd/averageWithArgs/averageWithArgs.go diff --git a/headFirstCodeMagnets/variadic.go b/cmd/headFirstCodeMagnets/variadic.go similarity index 100% rename from headFirstCodeMagnets/variadic.go rename to cmd/headFirstCodeMagnets/variadic.go diff --git a/playground/averageHeadFirstSolution.go b/cmd/playground/averageHeadFirstSolution.go similarity index 84% rename from playground/averageHeadFirstSolution.go rename to cmd/playground/averageHeadFirstSolution.go index 0c27a32..c6787fa 100644 --- a/playground/averageHeadFirstSolution.go +++ b/cmd/playground/averageHeadFirstSolution.go @@ -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() { diff --git a/playground/averageHeadFirstVariadic.go b/cmd/playground/averageHeadFirstVariadic.go similarity index 100% rename from playground/averageHeadFirstVariadic.go rename to cmd/playground/averageHeadFirstVariadic.go diff --git a/playground/averageMySolution.go b/cmd/playground/averageMySolution.go similarity index 94% rename from playground/averageMySolution.go rename to cmd/playground/averageMySolution.go index 02f0e85..f706eee 100644 --- a/playground/averageMySolution.go +++ b/cmd/playground/averageMySolution.go @@ -8,7 +8,7 @@ import ( "strconv" "strings" - "git.linuxhg.com/Go_Training/keyboard" + "trying_out_go/pkg/keyboard" ) func averageMySolution() { diff --git a/playground/averageVariadic.go b/cmd/playground/averageVariadic.go similarity index 100% rename from playground/averageVariadic.go rename to cmd/playground/averageVariadic.go diff --git a/playground/calendar.go b/cmd/playground/calendar.go similarity index 90% rename from playground/calendar.go rename to cmd/playground/calendar.go index 9dd1074..c94f44f 100644 --- a/playground/calendar.go +++ b/cmd/playground/calendar.go @@ -3,7 +3,7 @@ package main import ( "fmt" - "git.linuxhg.com/Go_Training/calendar" + "trying_out_go/pkg/calendar" ) func companyCalendar() { diff --git a/playground/countVotes.go b/cmd/playground/countVotes.go similarity index 92% rename from playground/countVotes.go rename to cmd/playground/countVotes.go index 10a910d..23af625 100644 --- a/playground/countVotes.go +++ b/cmd/playground/countVotes.go @@ -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() { diff --git a/cmd/playground/directory.go b/cmd/playground/directory.go new file mode 100644 index 0000000..d48afc6 --- /dev/null +++ b/cmd/playground/directory.go @@ -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") +} diff --git a/playground/double.go b/cmd/playground/double.go similarity index 100% rename from playground/double.go rename to cmd/playground/double.go diff --git a/playground/error.go b/cmd/playground/error.go similarity index 100% rename from playground/error.go rename to cmd/playground/error.go diff --git a/playground/fuel.go b/cmd/playground/fuel.go similarity index 100% rename from playground/fuel.go rename to cmd/playground/fuel.go diff --git a/playground/guess.go b/cmd/playground/guess.go similarity index 100% rename from playground/guess.go rename to cmd/playground/guess.go diff --git a/playground/magazineSubscribers.go b/cmd/playground/magazineSubscribers.go similarity index 95% rename from playground/magazineSubscribers.go rename to cmd/playground/magazineSubscribers.go index 089de05..aab0a04 100644 --- a/playground/magazineSubscribers.go +++ b/cmd/playground/magazineSubscribers.go @@ -3,7 +3,7 @@ package main import ( "fmt" - "git.linuxhg.com/Go_Training/magazine" + "trying_out_go/pkg/magazine" ) func printInfo(s *magazine.Subscriber) { diff --git a/playground/myIntPointer.go b/cmd/playground/myIntPointer.go similarity index 100% rename from playground/myIntPointer.go rename to cmd/playground/myIntPointer.go diff --git a/playground/passfail.go b/cmd/playground/passfail.go similarity index 88% rename from playground/passfail.go rename to cmd/playground/passfail.go index d76ced9..0669a24 100644 --- a/playground/passfail.go +++ b/cmd/playground/passfail.go @@ -4,7 +4,7 @@ import ( "fmt" "log" - "git.linuxhg.com/Go_Training/keyboard" + "trying_out_go/pkg/keyboard" ) func passFail() { diff --git a/playground/playground.go b/cmd/playground/playground.go similarity index 96% rename from playground/playground.go rename to cmd/playground/playground.go index ff9e980..fb687a5 100644 --- a/playground/playground.go +++ b/cmd/playground/playground.go @@ -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 { diff --git a/playground/shopping.go b/cmd/playground/shopping.go similarity index 100% rename from playground/shopping.go rename to cmd/playground/shopping.go diff --git a/playground/slices.go b/cmd/playground/slices.go similarity index 100% rename from playground/slices.go rename to cmd/playground/slices.go diff --git a/playground/test.go b/cmd/playground/test.go similarity index 100% rename from playground/test.go rename to cmd/playground/test.go diff --git a/playground/toCelsius.go b/cmd/playground/toCelsius.go similarity index 87% rename from playground/toCelsius.go rename to cmd/playground/toCelsius.go index dbb7499..994eba0 100644 --- a/playground/toCelsius.go +++ b/cmd/playground/toCelsius.go @@ -4,7 +4,7 @@ import ( "fmt" "log" - "git.linuxhg.com/Go_Training/keyboard" + "trying_out_go/pkg/keyboard" ) func toCelsius() { diff --git a/playground/variadic.go b/cmd/playground/variadic.go similarity index 100% rename from playground/variadic.go rename to cmd/playground/variadic.go diff --git a/playground/wallArea.go b/cmd/playground/wallArea.go similarity index 100% rename from playground/wallArea.go rename to cmd/playground/wallArea.go diff --git a/cmd/sum/sum.go b/cmd/sum/sum.go new file mode 100644 index 0000000..6bb43ef --- /dev/null +++ b/cmd/sum/sum.go @@ -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) +} diff --git a/playground/go.mod b/go.mod similarity index 79% rename from playground/go.mod rename to go.mod index 1261c6f..34ba924 100644 --- a/playground/go.mod +++ b/go.mod @@ -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 diff --git a/playground/go.sum b/go.sum similarity index 100% rename from playground/go.sum rename to go.sum diff --git a/pkg/calendar/date.go b/pkg/calendar/date.go new file mode 100644 index 0000000..c9b9600 --- /dev/null +++ b/pkg/calendar/date.go @@ -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 +} diff --git a/pkg/calendar/event.go b/pkg/calendar/event.go new file mode 100644 index 0000000..a7d3c26 --- /dev/null +++ b/pkg/calendar/event.go @@ -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 +} diff --git a/pkg/datafile/floats.go b/pkg/datafile/floats.go new file mode 100644 index 0000000..157d0ec --- /dev/null +++ b/pkg/datafile/floats.go @@ -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 +} diff --git a/pkg/datafile/strings.go b/pkg/datafile/strings.go new file mode 100644 index 0000000..0a5e8a5 --- /dev/null +++ b/pkg/datafile/strings.go @@ -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 +} diff --git a/pkg/date/date.go b/pkg/date/date.go new file mode 100644 index 0000000..c9b9600 --- /dev/null +++ b/pkg/date/date.go @@ -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 +} diff --git a/pkg/date/event.go b/pkg/date/event.go new file mode 100644 index 0000000..a7d3c26 --- /dev/null +++ b/pkg/date/event.go @@ -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 +} diff --git a/pkg/gadget/program.go b/pkg/gadget/program.go new file mode 100644 index 0000000..4e84f6c --- /dev/null +++ b/pkg/gadget/program.go @@ -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{}) +} diff --git a/pkg/gadget/tape.go b/pkg/gadget/tape.go new file mode 100644 index 0000000..d294fe3 --- /dev/null +++ b/pkg/gadget/tape.go @@ -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") +} diff --git a/pkg/keyboard/keyboard.go b/pkg/keyboard/keyboard.go new file mode 100644 index 0000000..4f04394 --- /dev/null +++ b/pkg/keyboard/keyboard.go @@ -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 +} diff --git a/pkg/magazine/types.go b/pkg/magazine/types.go new file mode 100644 index 0000000..b53aa2e --- /dev/null +++ b/pkg/magazine/types.go @@ -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 +}