From ed47468ebc355165d243b5643bc050432947c5e8 Mon Sep 17 00:00:00 2001 From: hthuz Date: Mon, 1 Jul 2024 09:30:40 +0800 Subject: [PATCH] add: go interface --- go/interface.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 go/interface.go diff --git a/go/interface.go b/go/interface.go new file mode 100644 index 0000000..2bb2b84 --- /dev/null +++ b/go/interface.go @@ -0,0 +1,32 @@ +package main + +import "fmt" + +type Animal interface { + Bark() +} + +type Dog struct { + name string +} + +type Cat struct { + name string +} + +func (d Dog) Bark() { + fmt.Println("W") +} + +func (c Cat) Bark() { + fmt.Println("C") +} + +func main() { + var d = Dog{"dog"} + var c = Cat{"cat"} + var in_d = Animal(d) + var in_c = Animal(c) + in_d.Bark() + in_c.Bark() +}