-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface_test.go
66 lines (52 loc) · 867 Bytes
/
interface_test.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
63
64
65
66
package main
import (
"fmt"
"log"
"testing"
)
func TestInterface(t *testing.T) {
log.Println("test interface")
type test struct {
Name string
age int
}
add := func(num ...interface{}) {
log.Println("add")
}
slice := []*test{&test{"asai", 28}, &test{"asai", 29}, &test{"asai", 30}}
//
//
add(slice)
add([]*test{&test{"asai", 28}})
}
type square struct {
r int
}
type circle struct {
r int
}
func TestHttp1(t *testing.T) {
}
func (s square) area() int {
return s.r * s.r
}
func (c circle) area() int {
return c.r * 3
}
func TestInterface2(t *testing.T) {
log.Println(fmt.Sprintf("\\%s", "+123"))
s := square{1}
c := circle{1}
a := [2]interface{}{s, c}
fmt.Println(s, c, a)
sum := 0
for _, t := range a {
switch v := t.(type) {
case square:
sum += v.area()
case circle:
sum += v.area()
}
}
fmt.Println(sum)
}