回答編集履歴
1
サンプルコードの修正をしました
answer
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
サンプルコード的なものを書いてみました
|
2
|
-
https://play.golang.org/p/
|
2
|
+
[https://play.golang.org/p/TDJTvw-3O_8](https://play.golang.org/p/TDJTvw-3O_8)
|
3
3
|
|
4
4
|
インターフェースを実装したストラクトを利用すると便利さがわかるかもしれません
|
5
5
|
|
@@ -10,42 +10,42 @@
|
|
10
10
|
"fmt"
|
11
11
|
)
|
12
12
|
|
13
|
-
//
|
13
|
+
// Human interface
|
14
|
-
type
|
14
|
+
type Human interface {
|
15
|
-
|
15
|
+
SayHello()
|
16
|
-
sub() int64
|
17
|
-
mul() int64
|
18
|
-
div() int64
|
19
16
|
}
|
20
17
|
|
18
|
+
// Japanese struct
|
21
|
-
type
|
19
|
+
type Japanese struct {
|
20
|
+
Name string
|
22
|
-
|
21
|
+
Age int64
|
23
|
-
Y int64
|
24
22
|
}
|
25
23
|
|
26
|
-
|
24
|
+
// American struct
|
27
|
-
|
25
|
+
type American struct {
|
26
|
+
Name string
|
27
|
+
Age int64
|
28
28
|
}
|
29
29
|
|
30
|
+
// SayHello void (Japanese)
|
30
|
-
func (
|
31
|
+
func (japanese *Japanese) SayHello() {
|
31
|
-
|
32
|
+
fmt.Printf("%s(%d):\tこんにちは\n", japanese.Name, japanese.Age)
|
32
33
|
}
|
33
34
|
|
35
|
+
// SayHello void (American)
|
34
|
-
func (
|
36
|
+
func (american *American) SayHello() {
|
35
|
-
|
37
|
+
fmt.Printf("%s(%d):\tHello\n", american.Name, american.Age)
|
36
38
|
}
|
37
39
|
|
38
|
-
func (calc *IntegerCalc) div() int64 {
|
39
|
-
return int64(calc.X / calc.Y)
|
40
|
-
}
|
41
|
-
|
42
40
|
func main() {
|
43
|
-
|
41
|
+
humans := []Human{}
|
44
42
|
|
45
|
-
fmt.Printf("%d + %d = %d\n", intCalc.X, intCalc.Y, intCalc.add())
|
46
|
-
fmt.Printf("%d - %d = %d\n", intCalc.X, intCalc.Y, intCalc.sub())
|
47
|
-
fmt.Printf("%d * %d = %d\n", intCalc.X, intCalc.Y, intCalc.mul())
|
48
|
-
|
43
|
+
humans = append(humans, &Japanese{Name: "東京太郎", Age: int64(30)})
|
44
|
+
humans = append(humans, &American{Name: "Bob", Age: int64(25)})
|
45
|
+
|
46
|
+
for _, human := range humans {
|
47
|
+
human.SayHello()
|
48
|
+
}
|
49
49
|
}
|
50
50
|
|
51
51
|
```
|