-
Notifications
You must be signed in to change notification settings - Fork 56
/
main.go
69 lines (57 loc) · 1.88 KB
/
main.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
67
68
69
package main
import (
"fmt"
)
//类型断言
//类型断言,由于接口是一般类型,不知道具体类型,如果要转成具体类型,就需要使用类型断言
type Student struct {
Name string
}
func main() {
var x interface{}
var b2 float32 = 1.1
x = b2 //空接口 可以接受任意值
//y := x.(float32)
//fmt.Printf("y的类型是%T 值是%v", y, y)
//在进行类型断言时,如果类型不匹配,就会报panic, 因此进行类型断言时,要确保原来的空接口指向的就是断言的类型
//如何在进行断言时,带上检测机制,如果成功就 ok,否则也不要报 panic
if y, ok := x.(float32); ok {
fmt.Println("convert success")
fmt.Printf("y的类型是%T 值是%v", y, y) } else {
fmt.Println("convert fail")
}
fmt.Println("继续执行")
var n1 float32 = 1.1
var n2 float64 = 2.2
var n3 int32 = 30
var name string = "tom"
address := "北京"
n4 := 300
s1 := Student{"xiao"}
s2 := &Student{"tom"}
TypeJudge(n1, n2, n3, name, address, n4, s1, s2)
}
//类型断言最佳实践2
//写一函数,循环判断传入参数的类型
func TypeJudge(items...interface{}) {
for index, v := range(items) {
switch v.(type) {
case bool:
fmt.Printf("第%v个参数是bool类型,值是%v\n", index, v)
case float32:
fmt.Printf("第%v个参数是float32类型,值是%v\n", index, v)
case float64:
fmt.Printf("第%v个参数是float64类型,值是%v\n", index, v)
case int, int32, int64:
fmt.Printf("第%v个参数是整数类型,值是%v\n", index, v)
case string:
fmt.Printf("第%v个参数是字符串类型,值是%v\n", index, v)
case Student:
fmt.Printf("第%v个参数是Student类型,值是%v\n", index, v)
case *Student:
fmt.Printf("第%v个参数是*Student类型,值是%v\n", index, v)
default:
fmt.Printf("第%v个参数不确定类型,值是%v\n", index, v)
}
}
}