-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module shuffler | ||
|
||
go 1.22.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |