回答編集履歴

1

サンプルコードの修正をしました

2019/05/24 18:37

投稿

teikoku-penguin
teikoku-penguin

スコア314

test CHANGED
@@ -1,6 +1,6 @@
1
1
  サンプルコード的なものを書いてみました
2
2
 
3
- https://play.golang.org/p/rBkWq4yWMEk
3
+ [https://play.golang.org/p/TDJTvw-3O_8](https://play.golang.org/p/TDJTvw-3O_8)
4
4
 
5
5
 
6
6
 
@@ -22,59 +22,55 @@
22
22
 
23
23
 
24
24
 
25
- // Calcインターフェース
25
+ // Human interface
26
26
 
27
- type Calc interface {
27
+ type Human interface {
28
28
 
29
- add() int64
29
+ SayHello()
30
-
31
- sub() int64
32
-
33
- mul() int64
34
-
35
- div() int64
36
30
 
37
31
  }
38
32
 
39
33
 
40
34
 
41
- type IntegerCalc struct {
35
+ // Japanese struct
42
36
 
43
- X int64
37
+ type Japanese struct {
44
38
 
39
+ Name string
40
+
45
- Y int64
41
+ Age int64
46
42
 
47
43
  }
48
44
 
49
45
 
50
46
 
51
- func (calc *IntegerCalc) add() int64 {
47
+ // American struct
52
48
 
53
- return calc.X + calc.Y
49
+ type American struct {
50
+
51
+ Name string
52
+
53
+ Age int64
54
54
 
55
55
  }
56
56
 
57
57
 
58
58
 
59
- func (calc *IntegerCalc) sub() int64 {
59
+ // SayHello void (Japanese)
60
60
 
61
- return calc.X - calc.Y
61
+ func (japanese *Japanese) SayHello() {
62
+
63
+ fmt.Printf("%s(%d):\tこんにちは\n", japanese.Name, japanese.Age)
62
64
 
63
65
  }
64
66
 
65
67
 
66
68
 
67
- func (calc *IntegerCalc) mul() int64 {
69
+ // SayHello void (American)
68
70
 
69
- return calc.X * calc.Y
71
+ func (american *American) SayHello() {
70
72
 
71
- }
72
-
73
-
74
-
75
- func (calc *IntegerCalc) div() int64 {
76
-
77
- return int64(calc.X / calc.Y)
73
+ fmt.Printf("%s(%d):\tHello\n", american.Name, american.Age)
78
74
 
79
75
  }
80
76
 
@@ -82,17 +78,21 @@
82
78
 
83
79
  func main() {
84
80
 
85
- intCalc := &IntegerCalc{X: int64(10), Y: int64(5)}
81
+ humans := []Human{}
86
82
 
87
83
 
88
84
 
89
- fmt.Printf("%d + %d = %d\n", intCalc.X, intCalc.Y, intCalc.add())
85
+ humans = append(humans, &Japanese{Name: "東京太郎", Age: int64(30)})
90
86
 
91
- fmt.Printf("%d - %d = %d\n", intCalc.X, intCalc.Y, intCalc.sub())
87
+ humans = append(humans, &American{Name: "Bob", Age: int64(25)})
92
88
 
93
- fmt.Printf("%d * %d = %d\n", intCalc.X, intCalc.Y, intCalc.mul())
94
89
 
90
+
95
- fmt.Printf("%d / %d = %d\n", intCalc.X, intCalc.Y, intCalc.div())
91
+ for _, human := range humans {
92
+
93
+ human.SayHello()
94
+
95
+ }
96
96
 
97
97
  }
98
98