xxxxxxxxxx
type MathInterface interface {
Added() int
Subtract() int
}
type Math struct {
x, y int
}
func (m *Math) Add() int {
return m.x + m.y
}
func (m *Math) Subtract() int {
return m.x - m.y
}
type HandlerMath struct {
handler *Math
}
func NewMatch(math *Math) *HandlerMath {
return &HandlerMath{handler: math}
}
func main() {
data := NewMatch(&Math{})
data.handler.x = 5
data.handler.y = 3
added := data.handler.Add()
subtract := data.handler.Subtract()
fmt.Println(added)
fmt.Println(subtract)
}
xxxxxxxxxx
package main
import (
"fmt"
)
type animal interface {
description() string
}
type cat struct {
Type string
Sound string
}
type snake struct {
Type string
Poisonous bool
}
func (s snake) description() string {
return fmt.Sprintf("Poisonous: %v", s.Poisonous)
}
func (c cat) description() string {
return fmt.Sprintf("Sound: %v", c.Sound)
}
func main() {
var a animal
a = snake{Poisonous: true}
fmt.Println(a.description())
a = cat{Sound: "Meow!!!"}
fmt.Println(a.description())
}
//=> Poisonous: true
//=> Sound: Meow!!!
xxxxxxxxxx
type MathInterface interface {
Added() int
Subtract() int
}
type Math struct {
x, y int
}
func (m *Math) Add() int {
return m.x + m.y
}
func (m *Math) Subtract() int {
return m.x - m.y
}
func main() {
data := Math{}
data.x = 5
data.y = 3
added := data.Add()
subtract := data.Subtract()
fmt.Println(added)
fmt.Println(subtract)
}
xxxxxxxxxx
type MathInterface interface {
Added() int
Subtract() int
}
type Math struct {
x, y int
}
func (m *Math) Add() int {
return m.x + m.y
}
func (m *Math) Subtract() int {
return m.x - m.y
}
func main() {
data := Math{}
data.x = 5
data.y = 3
added := data.Add()
subtract := data.Subtract()
fmt.Println(added)
fmt.Println(subtract)
}