forked from aslanvaroqua/goInterfaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (45 loc) · 830 Bytes
/
main.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
package main
import (
"fmt"
"log"
)
type Shape interface {
Area() int
}
type Rectangle struct {
length, width int
}
func (r Rectangle) Area() int {
return Multiply(r.length, r.width)
}
func Multiply(x int, y int) int {
return x * y
}
func IsShape(r Rectangle) (bool, error) {
defer func() {
if err := recover(); err != nil {
log.Println("work failed:", err)
}
}()
// Check if r is also Shape
_, ok := interface{}(r).(Shape)
if ok == false {
panic(ok)
} else {
fmt.Printf("r is a Rectangle and Rectangle is a shape \n")
}
return ok, nil
}
func MustGet(value bool, err error) bool {
i, err := value, err
if err != nil {
panic(err)
}
return i
}
func main() {
r := Rectangle{length:5, width:3}
if MustGet(IsShape(r)) {
fmt.Printf("Inheritance in GO successful through compilation")
}
}