diff --git a/go.mod b/go.mod index e9ce053..002b262 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module structs -go 1.21.0 +go 1.21.1 diff --git a/main.go b/main.go index af1ae19..b46572c 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,10 @@ package main +import ( + "fmt" + "structs/zoo" +) + /* 0. Declaration 1. Different types @@ -19,7 +24,19 @@ package main 16. Assignment */ +func main() { + cage1 := zoo.Cage{Size: "10x10", Color: "green", Number: 1, Animal: zoo.Animal{Kind: "Leon", Name: "Pusic", Number_of_legs: 4, Predator: true}} + cages := []zoo.Cage{cage1, {Size: "20x20", Color: "red", Number: 2, Animal: zoo.Animal{Kind: "mouse", Name: "Miki", Number_of_legs: 4, Predator: false}}} + + leon := zoo.Animal{Kind: "Elephant", Name: "White", Number_of_legs: 4, Predator: false} + cages = append(cages, zoo.Cage{Size: "10x10", Color: "green", Number: 1, Animal: leon}) + + fmt.Printf("On duty today: %s, with %d years of experience\n", zoo.Mihalich.Name, zoo.Mihalich.Experience) + fmt.Printf("We have a %v cages\n", len(cages)) + for i := 0; i < len(cages); i++ { + fmt.Printf("In cage: %v cages, we have %s, named %s. And ", i+1, cages[i].Animal.Kind, cages[i].Animal.Name) + cages[i].Animal.Food() + } -func main() { } diff --git a/structs/0_declaration.go b/structs/0_declaration.go index ff09605..623a334 100644 --- a/structs/0_declaration.go +++ b/structs/0_declaration.go @@ -8,6 +8,13 @@ type user struct { Age int } +type User struct { + // Contains different field names with different types + Name string + Surname string + Age int +} + // Public struct type Post struct { // Public field diff --git a/zoo/zoo.go b/zoo/zoo.go new file mode 100644 index 0000000..d3a33df --- /dev/null +++ b/zoo/zoo.go @@ -0,0 +1,35 @@ +package zoo + +// Private struct, visible inside this package only +type zookeeper struct { + // Contains different field names with different types + Name string + Surname string + Age int + Experience int +} + +type Animal struct { + Kind string + Name string + Number_of_legs int + Predator bool +} + +type Cage struct { + Size string + Color string + Number int + Animal Animal +} + +func (a *Animal) Food() { + if a.Predator == true { + println("it likes meat") + } else { + println("it likes apple") + } + +} + +var Mihalich = zookeeper{"Mihail", "Ivanov", 56, 30}