大佬教程收集整理的这篇文章主要介绍了将interface {}转换为int在Golang,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这里是错误消息:
...cAnnot convert val (type interface {}) to type int: need type assertion@H_197_4@代码:
var f interface{} err = json.Unmarshal([]byte(jsonStr),&f) if err != nil { utility.CreateErrorResponse(w,"Error: Failed to parse JSON data.") return } m := f.(map[String]interface{}) val,ok := m["area_id"] if !ok { utility.CreateErrorResponse(w,"Error: Area ID is missing from submitted data.") return } fmt.Fprintf(w,"Type = %v",val) // <--- Type = float64 iAreaId := int(val) // <--- Error on this line. testName := "Area_" + iAreaId // not reaching here@H_197_4@任何帮助将不胜感激。
iAreaId := int(val)@H_197_4@你想要一个type assertion:
iAreaId := val.(int) iAreaId,ok := val.(int) // Alt. non panicking version@H_197_4@为什么你不能convert一个接口类型的值的原因是引用规范部分中的这些规则:
… …
但
iAreaId := int(val)@H_197_4@不是任何一种情况1.-7。
以上是大佬教程为你收集整理的将interface {}转换为int在Golang全部内容,希望文章能够帮你解决将interface {}转换为int在Golang所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。