Compare commits
8 Commits
34b69520f3
...
main
Author | SHA1 | Date | |
---|---|---|---|
ce125279ca | |||
f68fdf3d9b | |||
56fa94fce7 | |||
5cbc048b51 | |||
19b1581f10 | |||
943191e9dd | |||
3ccf860ffc | |||
810d704afb |
@ -1,3 +0,0 @@
|
||||
module averageWithArgs
|
||||
|
||||
go 1.21.3
|
@ -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
|
@ -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() {
|
@ -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...))
|
||||
}
|
@ -8,7 +8,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.linuxhg.com/Go_Training/keyboard"
|
||||
"trying_out_go/pkg/keyboard"
|
||||
)
|
||||
|
||||
func averageMySolution() {
|
24
cmd/playground/calendar.go
Normal file
24
cmd/playground/calendar.go
Normal 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())
|
||||
}
|
@ -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() {
|
28
cmd/playground/directory.go
Normal file
28
cmd/playground/directory.go
Normal 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
66
cmd/playground/fuel.go
Normal 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))
|
||||
}
|
@ -3,7 +3,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.linuxhg.com/Go_Training/magazine"
|
||||
"trying_out_go/pkg/magazine"
|
||||
)
|
||||
|
||||
func printInfo(s *magazine.Subscriber) {
|
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.linuxhg.com/Go_Training/keyboard"
|
||||
"trying_out_go/pkg/keyboard"
|
||||
)
|
||||
|
||||
func passFail() {
|
@ -110,6 +110,18 @@ func main() {
|
||||
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 {
|
@ -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]
|
@ -48,6 +48,7 @@ func (m testModel) View() string {
|
||||
}
|
||||
|
||||
func terminalTest() {
|
||||
//goland:noinspection SpellCheckingInspection,SpellCheckingInspection
|
||||
items := []list.Item{
|
||||
item{title: "Raspberry Pi’s", desc: "I have ’em all over my house"},
|
||||
item{title: "Nutella", desc: "It's good on toast"},
|
@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.linuxhg.com/Go_Training/keyboard"
|
||||
"trying_out_go/pkg/keyboard"
|
||||
)
|
||||
|
||||
func toCelsius() {
|
@ -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 {
|
@ -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
55
cmd/sum/sum.go
Normal 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)
|
||||
}
|
@ -3,10 +3,7 @@ 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
|
||||
@ -26,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
|
@ -1,19 +1,9 @@
|
||||
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=
|
||||
@ -62,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
56
pkg/calendar/date.go
Normal 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
23
pkg/calendar/event.go
Normal 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
32
pkg/datafile/floats.go
Normal 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
31
pkg/datafile/strings.go
Normal 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
56
pkg/date/date.go
Normal 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
23
pkg/date/event.go
Normal 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
33
pkg/gadget/program.go
Normal 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
31
pkg/gadget/tape.go
Normal 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
38
pkg/keyboard/keyboard.go
Normal 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
21
pkg/magazine/types.go
Normal 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
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
// part of chapter 9
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Liters float64
|
||||
type Gallons float64
|
||||
|
||||
func fuel() {
|
||||
var carFuel Gallons
|
||||
var busFuel Liters
|
||||
carFuel = Gallons(10.0)
|
||||
busFuel = Liters(240.0)
|
||||
fmt.Println(carFuel, busFuel)
|
||||
}
|
Reference in New Issue
Block a user