2 回答
TA贡献1852条经验 获得超7个赞
reflect你可以像这样使用这个包:
package main
import (
"fmt"
"math/big"
"reflect"
)
type Test struct {
t interface{}
}
func main() {
testVar1 := big.NewInt(0)
testVar2 := int64(1)
testVar3 := []byte("test")
testVar4 := true
testVar5 := []int{1, 2, 3, 4}
var testVar6 Test
Issue(testVar1)
Issue(testVar2)
Issue(testVar3)
Issue(testVar4)
Issue(testVar5)
Issue(testVar6)
}
func Issue(t interface{}) {
switch t.(type) {
case *big.Int:
fmt.Println("*big.Int")
case int8, int16, int32, int64:
fmt.Println("int8, int16, int32, int64")
case []byte:
fmt.Println("[]byte")
case bool:
fmt.Println("bool")
case interface{}:
rt := reflect.TypeOf(t)
switch rt.Kind() {
case reflect.Slice:
fmt.Println("Slice")
case reflect.Array:
fmt.Println("Array")
default:
fmt.Println("interface{}")
}
default:
fmt.Println("unsupported type")
}
}
- 2 回答
- 0 关注
- 131 浏览
添加回答
举报