-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfaceEx.go
62 lines (48 loc) · 938 Bytes
/
interfaceEx.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
62
package codesnippets
// interface example
import (
"fmt"
)
type Node interface {
SetValue(v int)
GetValue() int
}
type SLLNode struct {
next *SLLNode
value int
}
func (sNode *SLLNode) SetValue(v int) {
sNode.value = v
}
func (sNode *SLLNode) GetValue() int {
return sNode.value
}
func NewSLLNode() *SLLNode {
return new(SLLNode)
}
type PowerNode struct {
next *PowerNode
value int
}
func (sNode *PowerNode) SetValue(v int) {
sNode.value = v * 10
}
func (sNode *PowerNode) GetValue() int {
return sNode.value
}
func NewPowerNode() *PowerNode {
return new(PowerNode)
}
func interfaceEx() {
var node Node
node = NewSLLNode()
node.SetValue(4)
fmt.Println("Node is of value ", node.GetValue())
node = NewPowerNode()
node.SetValue(5)
fmt.Println("PowerNode is of value ", node.GetValue())
// node.(TYPE) verifies if implements this interface
if n, ok := node.(*PowerNode); ok {
fmt.Println(n.value)
}
}