首页 > 其他分享 >go panic interface conversion interface {} is float64, not int

go panic interface conversion interface {} is float64, not int

时间:2024-09-26 20:28:25浏览次数:7  
标签:conversion log err float64 int json interface

package main

import (
	"encoding/json"
	"log"
)

type Student struct {
	Sex string `json:"sex"`
	Age int `json:"age"`
}

func main() {
	s1 := &Student{
		Sex: "s1",
		Age: 20,
	}
	str1, err := json.Marshal(s1)
	if err != nil {
		log.Fatal(err)
	}

	var m map[string]interface{}
	err = json.Unmarshal(str1, &m)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("%d\n", m["age"].(int))
}

json反序列化成map时,数字默认是float64类型,不是int。

log.Printf("%d\n", int(m["age"].(float64)))

推荐指定结构体指针接收反序列化结果,不需要类型断言,逻辑更清晰。

标签:conversion,log,err,float64,int,json,interface
From: https://www.cnblogs.com/WJQ2017/p/18434247

相关文章

  • SBB Activity Context Interface (ACI) object 和 Generic Activity Context Interfac
    在JAINSLEE中,SBBActivityContextInterface(ACI)object和GenericActivityContextInterfaceobject的使用主要取决于应用场景的需求、活动的复杂性以及是否需要对特定活动类型进行精确控制。为了更好地理解它们的使用场景、选择依据以及如何在项目中使用,我将详......
  • Introduction to the YouTube to WAV Conversion Website
    Title:IntroductiontotheYouTubetoWAVConversionWebsite-www.youtubetowav.topIntoday'sdigitalage,whereweconsumeavastamountofmultimediacontent,havingtheabilitytoconvertvideosintodifferentformatscanbeextremelyuseful.One......
  • 兼收并蓄 TypeScript - 类: interface
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-类:interface示例如下:class\interface.ts{//接口用于定义对象的形状,这个是TypeScript的功能(JavaScript中没有)interfacePerson{readonlyid:number;//只读......
  • Java 接口interface
    目录1.定义接口2.实现接口3.多重继承4.抽象方法5.默认方法6.静态方法7.私有方法8.常量在Java中,接口(interface)是一种引用类型,它类似于类(class),但是与类不同的是,接口不能包含任何具体的方法实现(除了默认方法和静态方法之外)。接口定义了一组规则或行为,这些规则由实现该接......
  • Taobao API interface: keyword search product list data interface
    TaobaoAPIinterface:keywordsearchproductlistdatainterface——Ontheroadofgrowth,weareallfellowtravelers.IhopethisarticleabouttheTaobaoproductlistinformationinterfaceforproductselectioncanhelpyou.Ilookforwardtosharing......
  • EBS:OM Sales Order销售订单【Open Interface、Open API】
    21. OM Sales Order销售订单【Open Interface、Open API】21.1. 快速参考。参考点内容功能导航N: OM/Orders, Returns/Sales Order并发请求N: OM/View/Request/Order Import接口表oe_headers_iface_all/oe_lines_iface_all/oe_actions_iface_all/….APIoe_order_pub.pr......
  • SuperClassAndInterface
    packagecom.shrimpking.t5;/***CreatedbyIntelliJIDEA.**@Author:Shrimpking*@create2024/9/1216:03*/publicinterfaceA{}packagecom.shrimpking.t5;/***CreatedbyIntelliJIDEA.**@Author:Shrimpking*@create2024/9/1216:03......
  • Taobao API interface: Taobao product details data interface
    InterfaceOverviewCommoninterface:taobao.item_get,usedtoobtainproductdetailsdata.Returninformation:Youcanobtainalotofinformationabouttheproduct,suchasproductname,price,sales,evaluation,storeinformation,etc.Specificallyincl......
  • Taobao API interface: Get Taobao product details data based on product ID
    TaobaoproductdetailsdataAPIinterfaceisasetofinterfacesprovidedbyTaobaoOpenPlatformforobtainingdetailedinformationofTaobaoproducts.Throughtheseinterfaces,developerscanintegrateTaobaoproductdataintheirownapplicationsorsy......
  • win7连接手机提示ADB Interface 找不到驱动程序
    描述:准备在电脑上使用ADB命令调试手机时,发现手机驱动无法安装,如下图 解决方法:下载GoogleAndroid驱动程序链接:https://pan.baidu.com/s/16ZCp0w1DhDcf4FUNZkKs1Q?pwd=92qq提取码:92qq复制这段内容后打开百度网盘手机App,操作更方便哦1、打开计算机管理 2、更新ADB......