go int類型轉換string,通過反射類型對比

  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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章