Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GO-0001: init zoo #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module structs

go 1.21.0
go 1.21.1
19 changes: 18 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package main

import (
"fmt"
"structs/zoo"
)

/*
0. Declaration
1. Different types
Expand All @@ -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() {
}
7 changes: 7 additions & 0 deletions structs/0_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions zoo/zoo.go
Original file line number Diff line number Diff line change
@@ -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}