Go 函数中返回结构体指针
来源:stackoverflow
时间:2024-03-10 12:09:25 407浏览 收藏
一分耕耘,一分收获!既然都打开这篇《Go 函数中返回结构体指针》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!
问题内容
我有两个包含不同数据的公共结构,以及一个包含这两个公共结构之一的私有中间结构。我还有一个函数可以解组中间结构,确定它包含哪个公共结构,并返回两个公共结构之一。
我面临的问题是最后一个函数的返回值。最简单的情况是,我以为我可以返回 *struct{}
,但我的 ide 中不断出现类型不匹配的情况。
很抱歉发布的代码多于可能需要的代码,但我正在努力使其尽可能接近我正在处理的代码。
package main import ( "encoding/json" "errors" ) // These vars are some errors I'll use in the functions later on var ( errInvalidBase64 = errors.New("invalid base64") errInvalidStructType = errors.New("invalid struct type") ) // Struct1 public struct type Struct1 struct { FName string `json:"first-name"` LName string `json:"last-name"` } // Struct2 public struct type Struct2 struct { Date string `json:"date"` Items []int `json:"items"` } // intermediateStruct private struct // The Type field indicates which kind of struct Data contains (Struct1 or Struct2) // The Data field contains either Struct1 or Struct2 which was previously marshalled into JSON type intermediateStruct struct { Type structType Data []byte } // The following type and const are my understanding of an enum in Go // structType is a private type for the type of struct intermediateStruct contains type structType int // These public constants are just to keep my hands out of providing values for the different struct types const ( StructType1 structType = iota StructType2 ) // unmarshalStruct1 unmarshalls JSON []byte into a new Struct1 and returns a pointer to that struct func unmarshalStruct1(b []bytes) (*Struct1, error) { newStruct1 := new(Struct1) err := json.Unmarshal(b, newStruct1) if err != nil { return nil, errInvalidBase64 } return newStruct1, nil } // unmarshalStruct2 unmarshalls JSON []byte into a new Struct2 and returns a pointer to that struct func unmarshalStruct2(b []bytes) (*Struct2, error) { newStruct2 := new(Struct2) err := json.Unmarshal(b, newStruct2) if err != nil { return nil, errInvalidBase64 } return newStruct2, nil } // receiveStruct accepts *intermediateStruct who's Data field contains either Struct1 or Struct2 // This function needs to return either *Struct1 or *Struct2 and an error func receiveStruct(iStruct *intermediateStruct) (*struct{}, error) { switch iStruct.Type { case StructType1: struct1, err := unmarshalStruct1(iStruct.Data) if err != nil { return nil, err } // The following line is where I'm getting the type mismatch return struct1, nil case StructType2: struct2, err := unmarshalStruct2(iStruct.Data) if err != nil { return nil, err } // The following line is another type mismatch return struct2, nil default: return nil, errInvalidStructType } }
我知道有一种方法可以实现我想要实现的目标 - 我只是缺乏实现目标的经验/理解。
感谢您的所有意见!
正确答案
您的 unmarshallStruct
函数返回一个指向 Struct1
或 Struct2
类型的指针(取决于调用的函数版本)。因此,变量 Struct1
和 Struct2
分别是指向类型 Struct1
和 Struct2
的指针。两者都不是指向类型结构的指针(无论如何我必须添加它不是真正的 Go 类型)。结构体是一个关键字,有助于声明包含字段/属性的类型。
根据您对其余代码的使用情况,可以尝试以下任一方法:
- 正如 mkopriva 建议的那样,返回一个 interface{} 对象,但您需要使用类型断言来实际确保该对象
- 定义一个 Struct1 和 Struct2 都实现的接口,并返回指向该接口的指针
- 创建适用于 Struct1 或 Struct2 的单独函数。这并不一定像听起来那么糟糕,因为 Go 允许您以传递类型的方式传递函数(请参阅
sort
包中的Less()
函数的示例)。
以上就是《Go 函数中返回结构体指针》的详细内容,更多关于的资料请关注golang学习网公众号!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习