Golang 将 interface{} 转换为 struct
来源:Golang技术栈
2023-04-23 22:45:56
0浏览
收藏
学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《Golang 将 interface{} 转换为 struct》,以下内容主要包含golang等知识点,如果你正在学习或准备学习Golang,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!
问题内容
我想改进下面代码中的 getCustomerFromDTO 方法,我需要从 interface{} 创建一个结构,目前我需要将该接口编组为 byte[],然后将数组解组为我的结构 - 必须有更好的方法.
我的用例是我通过 rabbitmq 发送结构并发送它们,我使用这个通用的 DTO 包装器,它具有关于它们的附加域特定数据。当我从 rabbit mq 接收到 DTO 时,消息下方的一层将解组到我的 DTO,然后我需要从该 DTO 获取我的结构。
type Customer struct {
Name string `json:"name"`
}
type UniversalDTO struct {
Data interface{} `json:"data"`
// more fields with important meta-data about the message...
}
func main() {
// create a customer, add it to DTO object and marshal it
customer := Customer{Name: "Ben"}
dtoToSend := UniversalDTO{customer}
byteData, _ := json.Marshal(dtoToSend)
// unmarshal it (usually after receiving bytes from somewhere)
receivedDTO := UniversalDTO{}
json.Unmarshal(byteData, &receivedDTO)
//Attempt to unmarshall our customer
receivedCustomer := getCustomerFromDTO(receivedDTO.Data)
fmt.Println(receivedCustomer)
}
func getCustomerFromDTO(data interface{}) Customer {
customer := Customer{}
bodyBytes, _ := json.Marshal(data)
json.Unmarshal(bodyBytes, &customer)
return customer
}
正确答案
在解组 DTO 之前,将该Data字段设置为您期望的类型。
type Customer struct {
Name string `json:"name"`
}
type UniversalDTO struct {
Data interface{} `json:"data"`
// more fields with important meta-data about the message...
}
func main() {
// create a customer, add it to DTO object and marshal it
customer := Customer{Name: "Ben"}
dtoToSend := UniversalDTO{customer}
byteData, _ := json.Marshal(dtoToSend)
// unmarshal it (usually after receiving bytes from somewhere)
receivedCustomer := &Customer{}
receivedDTO := UniversalDTO{Data: receivedCustomer}
json.Unmarshal(byteData, &receivedDTO)
//done
fmt.Println(receivedCustomer)
}
如果在Data解组之前无法初始化 DTO 上的字段,则可以在解组之后使用类型断言。encoding/json将
unamrshalsinterface{}类型值打包到 a中map[string]interface{},因此您的代码将如下所示:
type Customer struct {
Name string `json:"name"`
}
type UniversalDTO struct {
Data interface{} `json:"data"`
// more fields with important meta-data about the message...
}
func main() {
// create a customer, add it to DTO object and marshal it
customer := Customer{Name: "Ben"}
dtoToSend := UniversalDTO{customer}
byteData, _ := json.Marshal(dtoToSend)
// unmarshal it (usually after receiving bytes from somewhere)
receivedDTO := UniversalDTO{}
json.Unmarshal(byteData, &receivedDTO)
//Attempt to unmarshall our customer
receivedCustomer := getCustomerFromDTO(receivedDTO.Data)
fmt.Println(receivedCustomer)
}
func getCustomerFromDTO(data interface{}) Customer {
m := data.(map[string]interface{})
customer := Customer{}
if name, ok := m["name"].(string); ok {
customer.Name = name
}
return customer
}
今天带大家了解了golang的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
版本声明
本文转载于:Golang技术栈 如有侵犯,请联系study_golang@163.com删除
标签 - break vs continue vs goto
- 上一篇
- 标签 - break vs continue vs goto
- 下一篇
- how to serialize/deserialize a map in go
查看更多
最新文章
-
- Golang · Go问答 | 24分钟前 | go · 环境变量 ·
- t.Parallel 测试共享环境变量的隔离方案
- 115浏览 收藏
-
- Golang · Go问答 | 47分钟前 |
- 测试缓存未失效时输入文件依赖的处理
- 174浏览 收藏
-
- Golang · Go问答 | 1小时前 | 依赖管理 · go · go work sync go work vendor Go workspace inconsistent vendoring Go依赖同步
- go work vendor 结果不一致的依赖同步步骤
- 192浏览 收藏
-
- Golang · Go问答 | 2小时前 | 权限 · go ·
- GOMODCACHE 权限错误的目录与环境变量排查
- 490浏览 收藏
-
- Golang · Go问答 | 3小时前 |
- 泛型方法返回具体类型导致推断失败的改法
- 355浏览 收藏
-
- Golang · Go问答 | 4小时前 |
- 泛型方法接口约束无法满足时的定位方法
- 177浏览 收藏
-
- Golang · Go问答 | 4小时前 |
- 泛型方法接收指针接收者时的调用限制
- 249浏览 收藏
-
- Golang · Go问答 | 4小时前 |
- json/v2 解码 null 到指针字段的兼容处理
- 235浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 403次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 479次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 490次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 436次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 262次使用
查看更多
相关文章
-
- GOLANG使用Context管理关联goroutine的方法
- 2022-12-28 193浏览
-
- 聊聊Go中的注释和godoc工具
- 2022-12-29 354浏览
-
- 聊聊Golang语言中的复数类型
- 2023-01-07 418浏览
-
- 浅析Golang中的浮点类型(float32和float64)
- 2022-12-23 161浏览
-
- 聊聊Golang中的整数类型
- 2022-12-24 209浏览

