Skip to content

Instantly share code, notes, and snippets.

@purwandi
Last active May 4, 2020 05:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purwandi/fbd4e8834433c190dc1cc78f97921af3 to your computer and use it in GitHub Desktop.
Save purwandi/fbd4e8834433c190dc1cc78f97921af3 to your computer and use it in GitHub Desktop.
Interface
package main
import (
"fmt"
)
type Mamalia interface {
}
type Animal struct {
Mamalia Mamalia
Name string
Transport interface{}
}
func main() {
a := &Animal{}
a.Name = "String"
a.Transport = "Sayap"
// fmt.Println(a)
fmt.Println(a.Transport)
b := &Animal{}
b.Name = "String"
b.Transport = 4
// fmt.Println(b)
fmt.Println(b.Transport)
}
package main
import (
"fmt"
)
type Makanan struct {
Name string
}
type Mamalia interface {
Move() string
Eat() Makanan
}
type Person struct{}
func (u *Person) Move() string {
return "Walk"
}
type IkanPaus struct{}
func (i *IkanPaus) Move() string {
return "Swim"
}
func CheckCanSwim(m Mamalia) (string, bool) {
if m.Move() == "Walk" {
return m.Move(), false
}
return m.Move(), true
}
func main() {
fmt.Println(CheckCanSwim(&Person{}))
fmt.Println(CheckCanSwim(&IkanPaus{}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment