Compare commits

...

5 Commits

38 changed files with 464 additions and 55 deletions

View File

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

View File

@ -3,7 +3,7 @@ package main
import "fmt"
func sum(numbers ...int) int {
var sum int = 0
var sum = 0
for _, number := range numbers {
sum += number

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

@ -14,7 +14,7 @@ func averageCalc(numbers ...float64) float64 {
return sum / float64(len(numbers))
}
func averageHeadFirstVariadic() {
func main() {
arguments := os.Args[1:]
var numbers []float64
for _, argument := range arguments {
@ -22,5 +22,5 @@ func averageHeadFirstVariadic() {
Error(err)
numbers = append(numbers, number)
}
fmt.Printf("Average: %0.2\n", averageCalc(numbers...))
fmt.Printf("Average: %0.2f\n", averageCalc(numbers...))
}

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

@ -51,7 +51,7 @@ func (m shoppingModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.cursor++
}
// The "enter" key and the spacebar (a literal space) toggle
// The "enter" key and the space bar (a literal space) toggle
// the selected state for the item that the cursor is pointing at.
case "enter", " ":
_, ok := m.selected[m.cursor]

View File

@ -48,6 +48,7 @@ func (m testModel) View() string {
}
func terminalTest() {
//goland:noinspection SpellCheckingInspection,SpellCheckingInspection
items := []list.Item{
item{title: "Raspberry Pis", desc: "I have em all over my house"},
item{title: "Nutella", desc: "It's good on toast"},

View File

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

View File

@ -6,13 +6,13 @@ import (
)
func maximum(numbers ...float64) float64 {
max := math.Inf(-1)
maxNumber := math.Inf(-1)
for _, number := range numbers {
if number > max {
max = number
if number > maxNumber {
maxNumber = number
}
}
return max
return maxNumber
}
func inRange(min float64, max float64, numbers ...float64) []float64 {

View File

@ -5,15 +5,15 @@ import "fmt"
func paintNeeded(width float64, height float64) (paintNeededCalculated float64, anError error) {
area := width * height
if width < 0.0 {
return 0, fmt.Errorf("a width of %0.2f is invalid.", width)
return 0, fmt.Errorf("a width of %0.2f is invalid", width)
}
if height < 0.0 {
return 0, fmt.Errorf("a height of %0.2f is invalid.", height)
return 0, fmt.Errorf("a height of %0.2f is invalid", height)
}
return area / 10.0, nil
}
func printListersNeeded(amount float64) {
func printLitersNeeded(amount float64) {
fmt.Printf("%.2f liters needed\n", amount)
}
@ -23,21 +23,21 @@ func wallArea() {
if err != nil {
fmt.Println(err)
} else {
printListersNeeded(amount)
printLitersNeeded(amount)
}
total += amount
amount, err = paintNeeded(-5.2, 3.5)
if err != nil {
fmt.Println(err)
} else {
printListersNeeded(amount)
printLitersNeeded(amount)
}
total += amount
amount, err = paintNeeded(5.2, 5.0)
if err != nil {
fmt.Println(err)
} else {
printListersNeeded(amount)
printLitersNeeded(amount)
}
total += amount
fmt.Printf("Total: %.2f liters\n", total)

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

@ -0,0 +1,55 @@
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")
err := file.Close()
if err != nil {
log.Fatal(err)
}
}
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,17 +3,13 @@ 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/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.25.0
github.com/charmbracelet/lipgloss v0.9.1
github.com/dixonwille/wmenu/v5 v5.1.0
)
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
@ -27,7 +23,7 @@ require (
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rivo/uniseg v0.4.6 // indirect
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect

View File

@ -1,27 +1,9 @@
git.linuxhg.com/Go_Training/calendar v0.0.0-20240131021259-9a867797a789 h1:NjOZBosOrGRV87iw30jOyH8UNKv7bVNxtix+BguskVA=
git.linuxhg.com/Go_Training/calendar v0.0.0-20240131021259-9a867797a789/go.mod h1:E6GYPXO26PAOFhtrQ3d9K+yG0j6e4nFORZT5zlD7HOc=
git.linuxhg.com/Go_Training/calendar v0.0.0-20240131145407-3e70d7233820 h1:/n9lYFbddduk4UoGW0Jh12synMzF02ciC3gCkrqph5g=
git.linuxhg.com/Go_Training/calendar v0.0.0-20240131145407-3e70d7233820/go.mod h1:E6GYPXO26PAOFhtrQ3d9K+yG0j6e4nFORZT5zlD7HOc=
git.linuxhg.com/Go_Training/calendar v0.0.0-20240131150458-66340adc88a8 h1:UTF7JbrCemR61hRKderfV3Au3/0CFDIIefp70YalEww=
git.linuxhg.com/Go_Training/calendar v0.0.0-20240131150458-66340adc88a8/go.mod h1:E6GYPXO26PAOFhtrQ3d9K+yG0j6e4nFORZT5zlD7HOc=
git.linuxhg.com/Go_Training/calendar v0.0.0-20240131153240-372e0ebc267d h1:wKAe1v8k2qmtJUo/FgE2xqdLRNN31u01bT9/FQGr0MI=
git.linuxhg.com/Go_Training/calendar v0.0.0-20240131153240-372e0ebc267d/go.mod h1:E6GYPXO26PAOFhtrQ3d9K+yG0j6e4nFORZT5zlD7HOc=
git.linuxhg.com/Go_Training/datafile v0.0.0-20240111160218-6989e96515a9 h1:jjPk3X9lZ6bZhsjBvEJUGyqfNalUDG0Lt5UvaPrKFtM=
git.linuxhg.com/Go_Training/datafile v0.0.0-20240111160218-6989e96515a9/go.mod h1:iRnsnz7UZZtIhCX3KTblxBxtEuvPZ8MMi5GDWGIt+dw=
git.linuxhg.com/Go_Training/keyboard v0.0.0-20240111160241-d208f095efce h1:S9raWDpQtH3at5D4Bm0bfnvPd3ATGGMuZ8eQMrLddFM=
git.linuxhg.com/Go_Training/keyboard v0.0.0-20240111160241-d208f095efce/go.mod h1:K5mAqLMDPmhy6GwFinntpdt4ha3uuVlZ/WKH/hOtOXY=
git.linuxhg.com/Go_Training/magazine v0.0.0-20240111162752-2b431f589215 h1:G95CQwU1Ni+VSgPH2yiWwI/e+r80j0i3kvthhgDTbsI=
git.linuxhg.com/Go_Training/magazine v0.0.0-20240111162752-2b431f589215/go.mod h1:H9ZFFH+6JXVuh3lnMJu6Nsr7c+gIi8f1dowZNeXJ0b8=
git.linuxhg.com/Go_Training/magazine v0.0.0-20240111171405-a42dd8b99dd0 h1:EvXU4Pjkf3iDQgpVIw0YbLZ9ri9YCP3FcbT3C5Uuc2E=
git.linuxhg.com/Go_Training/magazine v0.0.0-20240111171405-a42dd8b99dd0/go.mod h1:H9ZFFH+6JXVuh3lnMJu6Nsr7c+gIi8f1dowZNeXJ0b8=
git.linuxhg.com/Go_Training/magazine v0.0.0-20240112152452-7bd91fa7e6c2 h1:P0nAtf/NM7J/lk0YwuE1nESGMYyEnbiv2VXPHTvbxYw=
git.linuxhg.com/Go_Training/magazine v0.0.0-20240112152452-7bd91fa7e6c2/go.mod h1:H9ZFFH+6JXVuh3lnMJu6Nsr7c+gIi8f1dowZNeXJ0b8=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.17.1 h1:0SIyjOnkrsfDo88YvPgAWvZMwXe26TP6drRvmkjyUu4=
github.com/charmbracelet/bubbles v0.17.1/go.mod h1:9HxZWlkCqz2PRwsCbYl7a3KXvGzFaDHpYbSYMJ+nE3o=
github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0=
github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw=
github.com/charmbracelet/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM=
github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg=
github.com/charmbracelet/lipgloss v0.9.1 h1:PNyd3jvaJbg4jRHKWXnCj1akQm4rh8dbEzN1p/u1KWg=
@ -70,8 +52,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=
github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

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
}

View File

@ -93,5 +93,5 @@ Brian Martin
Amber Graham
Brian Martin
Amber Graham
Carloz Diaz
Carlos Diaz
Silly name