我正在尝试从 api 检索数据。当我从api获取数据时:
result, _ := s.getcases() // get cases via api fmt.println(reflect.typeof(result.records[0]["cases"]))
它显示结果的类型。records[0]["cases"] 是map[string]interface {}
但是,显然我不能直接使用这个作为地图。自:
fmt.println(reflect.typeof(result.records[0]["cases"]["id"]))
这将导致编译器错误:
立即学习“go语言免费学习笔记(深入)”;
invalid operation: cannot index result.Records[0]["Cases"] (map index expression of type interface{})
我可以知道如何在 golang 中使用这种类型吗?
该值的类型为 interface{},并且根据反射的输出,该接口中有一个 map[string]interface{} 值。因此,您必须使用类型断言获取存储在接口中的实际值:
fmt.Println(reflect.TypeOf(result.Records[0]["Cases"].(map[string]interface{})["Id"]))
以上就是我应该如何在golang中使用类型mapinterface {}的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号