-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path断言.go
61 lines (52 loc) · 1.1 KB
/
断言.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
package main
import "fmt"
type Point struct {
x int
y int
}
func main() {
fmt.Println("hello world")
var a interface{}
var point Point = Point{1, 2}
a = point
var b Point
// b = a不可以
b = a.(Point) // 接口断言进行类型转换。表示判断a是否是指向Point类型的变量,如果是就转成Point类型
fmt.Println(b)
var x interface{}
var b2 float32 = 1.1
x = b2
y, ok := x.(float32)
if ok == true {
fmt.Println("success")
fmt.Println(y)
} else {
fmt.Println("fail")
fmt.Println(y)
}
fmt.Println("continue")
TypeJudge(1, 1.1, x, y)
}
// TypeJudge 判断传入参数类型
func TypeJudge(items ...interface{}) {
fmt.Println("typeJudge....................")
for i, x := range items {
fmt.Println("x=", x)
switch x.(type) { //固定写法
case Point:
fmt.Println("Point:", i)
case int, int8, int16, int32, int64:
fmt.Println("int:", i)
case float32, float64:
fmt.Println("float32:", i)
case string:
fmt.Println("string:", i)
case bool:
fmt.Println("bool:", i)
case nil:
fmt.Println("nil:", i)
default:
fmt.Println("not found")
}
}
}