Reflection 反映[]字节的值
如何检索此接口的[]字节值Reflection 反映[]字节的值,reflection,go,Reflection,Go,如何检索此接口的[]字节值 package main import ( "reflect" ) func byteInterface() interface{} { return []byte("foo") } func main() { //var b []byte i := byteInterface() switch { case reflect.TypeOf(i).Kind() == reflect.Slice &&
package main
import (
"reflect"
)
func byteInterface() interface{} {
return []byte("foo")
}
func main() {
//var b []byte
i := byteInterface()
switch {
case reflect.TypeOf(i).Kind() == reflect.Slice && (reflect.TypeOf(i) == reflect.TypeOf([]byte(nil))):
default:
panic("should have bytes")
}
}
您可以为此使用一个;无需使用reflect
软件包:
package main
func byteInterface() interface{} {
return []byte("foo")
}
func main() {
i := byteInterface()
if b, ok := i.([]byte); ok {
// use b as []byte
println(len(b))
} else {
panic("should have bytes")
}
}
令人烦恼的简单。谢谢,到时候我会接受的。