learned I didn't need git for external modules and different folders can have different mains
This commit is contained in:
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
|
||||
}
|
Reference in New Issue
Block a user