-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabcd.go
45 lines (37 loc) · 838 Bytes
/
abcd.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
package main
import "fmt"
func main() {
// now construct different instances of structs
sa := SavingAccount{
amount: 123,
}
na := NormalAccount{
amount: 456,
}
// bc is an array of type balance_calculator which can handle all both type of data of na and sa
// because it hs implemented the methods of SavingAccount and NormalAccount
// so it can access the variables of both of them
bc := []balance_calculator{sa, na}
var total int
for _, v := range bc {
total = total + v.balance()
}
fmt.Println(total)
fmt.Println(sa.amount)
fmt.Println(na.amount)
}
type SavingAccount struct {
amount int
}
func (sa SavingAccount) balance() int {
return sa.amount
}
type NormalAccount struct {
amount int
}
func (na NormalAccount) balance() int {
return na.amount
}
type balance_calculator interface {
balance() int
}