4. What is "Object Oriented" (wikipedia)
A language is usually considered object-based if it includes the basic capabilities
for an object: identity, properties, and attributes
A language is considered object-oriented if it is object-based and also has the
capability of polymorphism and inheritance
11. What is "Inheritance" (wikipedia)
Provides reuse of existing objects
Classes are created in hierarchies
Inheritance passes knowledge down!
12. Inheritance is not good
In Java user group meeting,
James Gosling (Java’s inventor) says:
You should avoid implementation inheritance whenever possible.
13. Go's approach
Go avoided inheritance
Go strictly follows the Composition over inheritance principle
14. What is Composition
Provides reuse of Objects
One object is declared by containing other objects
Composition pulls knowledge into another
15. Composition in Go
// Type Declaration
type Item struct {
name string
price float64
quantity int
}
type DiscountItem struct {
ItemItem
discountRate float64
}
// In Action
func main() {
shoes := Item{name: "Sports Shoes", price: 30000, quantity: 2}
eventShoes := DiscountItem{eventShoes := DiscountItem{
Item{name: "Women's Walking Shoes", price: 50000, quantity: 3},Item{name: "Women's Walking Shoes", price: 50000, quantity: 3},
10.00,10.00,
}}
fmt.Println("shoes: ", shoes)
fmt.Println("eventShoes: ", eventShoes)
} Run
16. Call Method of Embedded Type
type DiscountItem struct {
Item
discountRate float64
}
// Method Declaration
func (t Item) Cost() float64 {func (t Item) Cost() float64 {
return t.price * float64(t.quantity)return t.price * float64(t.quantity)
}}
// In Action
func main() {
shoes := Item{name: "Sports Shoes", price: 30000, quantity: 2}
eventShoes := DiscountItem{
Item{name: "Women's Walking Shoes", price: 50000, quantity: 3},
10.00,
}
fmt.Println("cost of shoes: ", shoes.Cost())fmt.Println("cost of shoes: ", shoes.Cost())
fmt.Println("cost of eventShoes: ", eventShoes.Cost())fmt.Println("cost of eventShoes: ", eventShoes.Cost())
} Run
21. What is "Polymorphism" (wikipedia)
The provision of a single interface to entities of different types
Via Generics, Overloading and/or Subtyping
22. Go’s approach
Go avoided subtyping & overloading
Go does not provide Generics
Polymorphism via interfaces
23. Interfaces in Go
Interfaces are just sets of methods
Interfaces define behavior (duck typing)
"If something can do this, then it can be used here”
24. Interfaces in Go
type Rental struct {
name string
feePerDay float64
periodLength int
RentalPeriod
}
type RentalPeriod int
const (
Days RentalPeriod = iota
Weeks
Months
)
func (p RentalPeriod) ToDays() int {
switch p {
case Weeks:
return 7
case Months:
return 30
default:
return 1
}
}
30. Deep into Go's Standard Library
io.Writer interface
// http://godoc.org/io#Writer
type Writer interface {
Write(p []byte) (n int, err os.Error)
}
fmt.Fprintln function
func Fprintln(w io.Writer, a ...interface{}) (n int, err error)
31. The Power of Interfaces
In handle function, just write to io.Writer object
func handle(w io.Writer, msg string) {
fmt.Fprintln(w, msg)
}
The os.Stdout can be used for io.Writer.
func main() {
msg := []string{"hello", "world", "this", "is", "an", "example", "of", "io.Writer"}
for _, s := range msg {
time.Sleep(100 * time.Millisecond)
handle(os.Stdout, s)handle(os.Stdout, s)
}
} Run
32. The Power of Interfaces
The http.ResponseWriter can be used for io.Writer.
localhost:4000/hello-world(http://localhost:4000/hello-world)
localhost:4000/this-is-an-example-of-io.Writer(http://localhost:4000/this-is-an-example-of-io.Writer)
func handle(w io.Writer, msg string) {
fmt.Fprintln(w, msg)
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handle(w, r.URL.Path[1:])handle(w, r.URL.Path[1:])
})
fmt.Println("start listening on port 4000")
http.ListenAndServe(":4000", nil)
} Run