-
Notifications
You must be signed in to change notification settings - Fork 0
/
16_interface1.go
61 lines (48 loc) · 1.17 KB
/
16_interface1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import "fmt"
// run with commands:
// go run src\16_interface1.go
/*
standard interface paradigm:
type interfaceName interface {
func1(inputParams) (returnParams)
func2(inputParams) (returnParams)
func3(inputParams) (returnParams)
}
If the interfaceName is in uppercase, its a public interface.
If the function name is in uppercase, its a public function.
A public function can be accessed outside of the package,
otherwise, non-public function can only be accessed inside of
the package.
*/
type Cat interface {
CatchMouse()
}
type Dog interface {
Bark()
}
type CatDog struct {
Name string
}
// type CatDog implements functions in interface Cat
func (catDog *CatDog) CatchMouse() {
fmt.Printf("%s is catching mice!\n", catDog.Name)
}
// type CatDog implements functions in interface Dog
func (catDog *CatDog) Bark() {
fmt.Printf("%s is barking!\n", catDog.Name)
}
func main() {
// catDog is a pointer to CatDog instance
catDog := &CatDog{
Name: "Tom",
}
// declare Cat interface and point to CatDog type
var cat Cat
cat = catDog
cat.CatchMouse()
// declare Dog interface and point to CatDog type
var dog Dog
dog = catDog
dog.Bark()
}