Skip to content
Open
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
32 changes: 31 additions & 1 deletion hello/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,35 @@ import (
)

func main() {
fmt.Println("Hello from Go!")
fmt.Println("Define functions as methods of custom types")
poodle := Dog{"Poodle", 10, "woof!"}
fmt.Println(poodle)
fmt.Printf("%v\n", poodle)
fmt.Printf("Breed : %v\nweight : %v\n", poodle.Breed, poodle.weight)
poodle.weight = 9
fmt.Printf("Breed : %v\nweight : %v\n", poodle.Breed, poodle.weight)

poodle.Speak()
poodle.sound = "Arf !"
poodle.Speak()
poodle.SpeakThreeTimes()
}

// Dog in an explort struct
type Dog struct {
Breed string
weight int
sound string
}

// exported fumction as method with upper case caracter in the name
func (d Dog) Speak() {
fmt.Println(d.sound)
}

func (d Dog) SpeakThreeTimes() {
d.sound = fmt.Sprintf("%v %v %v", d.sound, d.sound, d.sound)
fmt.Println(d.sound)
}
//The ability to create custom methods for your own types,
// makes Go behave more like a fully Object Oriented language.