go int类型转换string,通过反射类型对比

来源:互联网 发布:java中方法的概念 编辑:程序博客网 时间:2024/06/05 00:35

  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



0 0
原创粉丝点击