Last active
May 4, 2020 05:13
-
-
Save purwandi/fbd4e8834433c190dc1cc78f97921af3 to your computer and use it in GitHub Desktop.
Interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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