Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Api 如何比较map[string]接口{}';s值字符串是否为空_Api_Go_Controller_Beego - Fatal编程技术网

Api 如何比较map[string]接口{}';s值字符串是否为空

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

如何比较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)
    }
}
使用“反射”确定值的基类型字符串是否:

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)
    }
}