1.在拼写string时,下面这种写法会报错:
mismatched types string and int
func GetErrorJsonData(status int, msg string) string {
jsons := "{\"status\":" +status+ ",\"message\":\"" + msg + "\"}"
return jsons
}
正确的写法为:需要引入"
strconv"包
func GetErrorJsonData(status int, msg string) string {
jsons := "{\"status\":" + strconv.Itoa(status) + ",\"message\":\"" + msg + "\"}"
return jsons
}
2.反射类型对比
var user map[string]interface{}
<pre name="code" class="html">user["username"]获取map中的参数
reflect.TypeOf(user["username"])//得到map中的类型
reflect.TypeOf(user["username"])==reflect.String//与string匹配,成功则true,否则false