Api 如何比较map[string]接口{}';s值字符串是否为空
如何比较map[string]接口{}的值字符串 使用a确定值是否为Api 如何比较map[string]接口{}';s值字符串是否为空,api,go,controller,beego,Api,Go,Controller,Beego,如何比较map[string]接口{}的值字符串 使用a确定值是否为字符串: for key, value := range m3 { if s, ok := value.(string); ok { fmt.Printf("%s is the string %q\n", key, s) } else { fmt.Printf("%s is not a string\n", key) } } 使用“反射”确定值的基类型字符串是否: typ
字符串
:
for key, value := range m3 {
if s, ok := value.(string); ok {
fmt.Printf("%s is the string %q\n", key, s)
} else {
fmt.Printf("%s is not a string\n", key)
}
}
使用“反射”确定值的基类型字符串是否:
type mystring string
m3 := map[string]interface{}{
"something": 1,
"brawba": "Bawrbawr",
"foo": mystring("bar"),
}
for key, value := range m3 {
if reflect.ValueOf(value).Kind() == reflect.String {
fmt.Printf("%s is a string with type %T and value %q\n", key, value, value)
} else {
fmt.Printf("%s is not a string\n", key)
}
}
您需要的是类型断言。请参阅。@AndySchweig请检查我更新的问题。我只想知道值是不是字符串
type mystring string
m3 := map[string]interface{}{
"something": 1,
"brawba": "Bawrbawr",
"foo": mystring("bar"),
}
for key, value := range m3 {
if reflect.ValueOf(value).Kind() == reflect.String {
fmt.Printf("%s is a string with type %T and value %q\n", key, value, value)
} else {
fmt.Printf("%s is not a string\n", key)
}
}