Skip to content

Commit

Permalink
lesson 6
Browse files Browse the repository at this point in the history
  • Loading branch information
bodrovis committed Jun 11, 2024
1 parent 347ecb1 commit 4db621c
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lesson_6/animals/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module animals

go 1.22.2

require golang.org/x/text v0.16.0
2 changes: 2 additions & 0 deletions lesson_6/animals/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
79 changes: 79 additions & 0 deletions lesson_6/animals/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"fmt"
"os"

"animals/pets"
)

func main() {
myCat := pets.Cat{
Animal: pets.Animal{Name: "mr. buttons"},
Age: 5,
IsAsleep: true,
}
myDog := pets.Dog{
Animal: pets.Animal{Name: "spot"},
Age: 6,
Weight: 30,
}

var feedToCat uint8 = 3
var feedToDog uint8 = 10

catFed, err := feed(&myCat, feedToCat)
if err != nil {
fmt.Fprintf(os.Stderr, "Error feeding cat: %v\n", err)
} else {
fmt.Println("Cat ate:", catFed)
}

fmt.Print("\n\n\t =====\n\n\n")

dogFed, err := feed(&myDog, feedToDog)
if err != nil {
fmt.Fprintf(os.Stderr, "Error feeding dog: %v\n", err)
} else {
fmt.Println("Dog ate:", dogFed)
}

fmt.Print("\n\n\t =====\n\n\n")

displayInfo(myCat)
displayInfo(myDog)
displayInfo(42)
}

func feed(animal pets.EaterWalker, amount uint8) (uint8, error) {
switch v := animal.(type) {
case *pets.Cat:
fmt.Println(v.GetName(), "is a cat aged", v.Age)
case *pets.Dog:
fmt.Println(v.GetName(), "is a dog aged", v.Age)
default:
fmt.Println("Unknown animal type")
}

fmt.Println("First, let's walk!")
fmt.Println(animal.Walk())

fmt.Println("Now, let's feed our", animal.GetName())

return animal.Eat(amount)
}

func displayInfo(i interface{}) {
switch v := i.(type) {
case string:
fmt.Println("This is a string:", v)
case int:
fmt.Println("This is an int:", v)
case pets.Cat:
fmt.Println("This is a Cat named:", v.Name, "and it is", v.Age, "years old")
case pets.Dog:
fmt.Println("This is a Dog named:", v.Name, "and weight:", v.Weight)
default:
fmt.Println("Unknown type")
}
}
22 changes: 22 additions & 0 deletions lesson_6/animals/pets/cat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pets

type Cat struct {
Animal
Age uint8
IsAsleep bool
}

func (c *Cat) Eat(amount uint8) (uint8, error) {
if c.IsAsleep {
return 0, &ActionError{Name: c.GetName(), Reason: "it is asleep"}
}

if amount > 5 {
return 0, newError("Cat can't eat that much", nil)
}
return amount, nil
}

func (c *Cat) Walk() string {
return "Cat is walking!"
}
23 changes: 23 additions & 0 deletions lesson_6/animals/pets/dog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pets

type Dog struct {
Animal
Age uint8
Weight uint8
IsAsleep bool
}

func (d *Dog) Eat(amount uint8) (uint8, error) {
if d.IsAsleep {
return 0, &ActionError{Name: d.Name, Reason: "it is asleep"}
}

if amount > 15 {
return 0, newError("Dog can't eat that much", nil)
}
return amount, nil
}

func (d *Dog) Walk() string {
return "Dog is walking!"
}
51 changes: 51 additions & 0 deletions lesson_6/animals/pets/pets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package pets

import (
"fmt"

"golang.org/x/text/cases"
"golang.org/x/text/language"
)

type Eater interface {
Eat(amount uint8) (uint8, error)
}

type Walker interface {
Walk() string
}

type Named interface {
GetName() string
}

type EaterWalker interface {
Eater
Walker
Named
}

type Animal struct {
Name string
}

type ActionError struct {
Name string
Reason string
}

func (e *ActionError) Error() string {
return fmt.Sprintf("%s cannot perform the action: %s", e.Name, e.Reason)
}

func (a *Animal) GetName() string {
caser := cases.Title(language.English)
return caser.String(a.Name)
}

func newError(msg string, err error) error {
if err != nil {
return fmt.Errorf("%s: %w", msg, err)
}
return fmt.Errorf(msg)
}
3 changes: 3 additions & 0 deletions lesson_6/shuffler/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module shuffler

go 1.22.2
48 changes: 48 additions & 0 deletions lesson_6/shuffler/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"math/rand"
"reflect"
)

type Question struct {
Country string `json:"country"`
Capital string `json:"capital"`
}

func Shuffle(slice interface{}) {
rv := reflect.ValueOf(slice)

if rv.Kind() != reflect.Slice {
fmt.Println("Shuffle: provided interface is not a slice type")
return
}

length := rv.Len()
swap := reflect.Swapper(slice)

rand.Shuffle(length, func(i, j int) {
swap(i, j)
})
}

func main() {
questions := []Question{
{Country: "France", Capital: "Paris"},
{Country: "Germany", Capital: "Berlin"},
{Country: "Italy", Capital: "Rome"},
}

fmt.Println("Before shuffle:", questions)
Shuffle(questions)
fmt.Println("After shuffle:", questions)

numbers := []int{1, 2, 3, 4, 5}

fmt.Println("Before shuffle:", numbers)
Shuffle(numbers)
fmt.Println("After shuffle:", numbers)

Shuffle(42)
}

0 comments on commit 4db621c

Please sign in to comment.