18.5 接口

(1)如何检测一个值v是否实现了接口Stringer

if v, ok := v.(Stringer); ok {
    fmt.Printf("implements String(): %s\n", v.String())
}
1
2
3

(2)如何使用接口实现一个类型分类函数:

func classifier(items ...interface{}) {
    for i, x := range items {
        switch x.(type) {
        case bool:
            fmt.Printf("param #%d is a bool\n", i)
        case float64:
            fmt.Printf("param #%d is a float64\n", i)
        case int, int64:
            fmt.Printf("param #%d is an int\n", i)
        case nil:
            fmt.Printf("param #%d is nil\n", i)
        case string:
            fmt.Printf("param #%d is a string\n", i)
        default:
            fmt.Printf("param #%d’s type is unknown\n", i)
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

链接

上次更新: 2019-8-15 23:11:49