Compare commits

...

11 Commits

38 changed files with 595 additions and 59 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

@@ -0,0 +1,24 @@
// from chapter 10
package main
import (
"fmt"
"trying_out_go/pkg/calendar"
)
func companyCalendar() {
event := calendar.Event{}
err := event.SetTitle("Mom's Birthday")
Error(err)
err = event.SetYear(2019)
Error(err)
err = event.SetMonth(5)
Error(err)
err = event.SetDay(27)
Error(err)
fmt.Println(event.Title())
fmt.Println(event.Year())
fmt.Println(event.Month())
fmt.Println(event.Day())
}

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")
}

66
cmd/playground/fuel.go Normal file
View File

@@ -0,0 +1,66 @@
// part of chapter 9
package main
import "fmt"
type Liters float64
func (l Liters) String() string {
return fmt.Sprintf("%0.2f L", l)
}
type Gallons float64
func (g Gallons) String() string {
return fmt.Sprintf("%0.2f gal", g)
}
type Milliliters float64
func (m Milliliters) String() string {
return fmt.Sprintf("%0.2f mL", m)
}
func (l Liters) ToGallons() Gallons {
return Gallons(l * 0.264)
}
func (m Milliliters) ToGallons() Gallons {
return Gallons(m * 0.000264)
}
func (g Gallons) ToLiters() Liters {
return Liters(g * 3.785)
}
func (g Gallons) ToMilliliters() Milliliters {
return Milliliters(g * 3785.41)
}
func fuel() {
// long version
//var carFuel Gallons
//var busFuel Liters
//carFuel = Gallons(10.0)
//busFuel = Liters(240.0)
// short version
soda := Liters(2)
fmt.Printf("%0.0f liters equals %0.3f gallons\n", soda, soda.ToGallons())
water := Milliliters(500)
fmt.Printf("%0.0f milliliters equals %0.3f gallons \n", water, water.ToGallons())
//carFuel := Gallons(10.0)
//busFuel := Liters(240.0)
//
////if you want to convert between types, save variable with correct calculation
//carFuel += ToGallons(Liters(40.0))
//busFuel += ToLiters(Gallons(30.0))
//fmt.Printf("Car Fuel: %0.1f gallons\n", carFuel)
//fmt.Printf("Bus Fuel: %0.1f liters\n", busFuel)
milk := Gallons(2)
fmt.Printf("%0.0f gallons equals %0.3f liters\n", milk, milk.ToLiters())
fmt.Printf("%0.0f gallons equals %0.3f milliliters\n", milk, milk.ToMilliliters())
fmt.Println(Gallons(12.09285934))
fmt.Println(Liters(12.09285934))
fmt.Println(Milliliters(12.09285934))
}

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

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

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

@@ -1,11 +1,9 @@
module trying_out_go
go 1.21.3
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
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
@@ -16,19 +14,19 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/daviddengcn/go-colortext v1.0.0 // indirect
github.com/dixonwille/wlog/v3 v3.0.1 // indirect
github.com/dixonwille/wlog/v3 v3.0.4 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
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.4.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

View File

@@ -1,26 +1,24 @@
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=
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=
github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9Pmr+v1VEawJl6I=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/daviddengcn/go-colortext v0.0.0-20180409174941-186a3d44e920/go.mod h1:dv4zxwHi5C/8AeI+4gX4dCWOIvNi7I6JCSX0HvlKPgE=
github.com/daviddengcn/go-colortext v1.0.0 h1:ANqDyC0ys6qCSvuEK7l3g5RaehL/Xck9EX8ATG8oKsE=
github.com/daviddengcn/go-colortext v1.0.0/go.mod h1:zDqEI5NVUop5QPpVJUxE9UO10hRnmkD5G4Pmri9+m4c=
github.com/dixonwille/wlog/v3 v3.0.1 h1:ViTSsNNndHlKW5S89x5O0KSTpTT9zdPqrkA/TZYY8+s=
github.com/dixonwille/wlog/v3 v3.0.1/go.mod h1:fPYZR9Ne5gFh3N8b3CuXVWHWxkY6Yg1wCeS3Km6Nc0I=
github.com/dixonwille/wlog/v3 v3.0.4 h1:YLV5QT+dVKunV/XdlsWlrbTAZp41Zq21lb3YI3KKfRw=
github.com/dixonwille/wlog/v3 v3.0.4/go.mod h1:wh5luTpI/rczzuFeEjjLglEUkeGBIK1wZa2VTtpLkwY=
github.com/dixonwille/wmenu/v5 v5.1.0 h1:sKBHDoQ945NRvK0Eitd0kHDYHl1IYOSr1sdCK9c+Qr0=
github.com/dixonwille/wmenu/v5 v5.1.0/go.mod h1:l6EGfXHaN6DPgv5+V5RY+cydAXJsj/oDZVczcdukPzc=
github.com/golangplus/bytes v0.0.0-20160111154220-45c989fe5450/go.mod h1:Bk6SMAONeMXrxql8uvOKuAZSu8aM5RUGv+1C6IJaEho=
@@ -35,8 +33,8 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
@@ -54,24 +52,31 @@ 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=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

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