2 回答

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
您會(huì)看到該Kind()方法返回 a reflect.Kind,它是以下之一:
type Kind uint
const (
Invalid Kind = iota
Bool
Int
Int8
Int16
Int32
Int64
Uint
Uint8
Uint16
Uint32
Uint64
Uintptr
Float32
Float64
Complex64
Complex128
Array
Chan
Func
Interface
Map
Ptr
Slice
String
Struct
UnsafePointer
)
所以你需要的案例是 likereflect.Bool而不是簡單的bool.

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用預(yù)定義的反射類型常量:
for _, field := range s.Fields() {
switch field.Kind() {
case reflect.Bool:
// do something
case reflect.String:
// do something
}
}
}
- 2 回答
- 0 關(guān)注
- 151 瀏覽
添加回答
舉報(bào)