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