从 Go 中的函数返回指向结构的指针
Returning a pointer to a struct from a function in Go
我有两个包含不同数据的 public 结构,以及一个包含两个 public 结构之一的私有中间结构。我还有一个函数可以解组中间结构,确定它包含哪个 public 结构,以及 return 两个 public 结构之一。
我面临的问题是最后一个函数的 return 值。最简单的是,我认为我可以 return *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
}
}
我知道有一种方法可以实现我想要实现的目标 - 我只是缺少实现目标的 experience/understanding。
感谢所有的输入!
您的 unmarshallStruct
函数 return 是指向类型 Struct1
或 Struct2
的指针(取决于调用的函数版本)。因此变量 struct1
和 struct2
分别是指向类型 Struct1
和 Struct2
的指针。也不是指向类型 struct 的指针(无论如何我必须添加它不是真正的 Go 类型)。结构是一个关键字,有助于声明包含 fields/attributes.
的类型
根据您对其余代码的考虑 use-cases,可以改为尝试以下任一方法:
- 正如 mkopriva 所建议的那样,return 一个 interface{} 对象,但您需要使用类型断言来实际确定该对象
- 定义一个 Struct1 和 Struct2 都实现的接口,return一个指向这个的指针
- 制作可与 Struct1 或 Struct2 一起使用的单独函数。这并不一定像听起来那么糟糕,因为 Go 允许您以与传递类型相同的方式传递函数(参见
sort
包中的 Less()
函数示例)。
我有两个包含不同数据的 public 结构,以及一个包含两个 public 结构之一的私有中间结构。我还有一个函数可以解组中间结构,确定它包含哪个 public 结构,以及 return 两个 public 结构之一。
我面临的问题是最后一个函数的 return 值。最简单的是,我认为我可以 return *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
}
}
我知道有一种方法可以实现我想要实现的目标 - 我只是缺少实现目标的 experience/understanding。
感谢所有的输入!
您的 unmarshallStruct
函数 return 是指向类型 Struct1
或 Struct2
的指针(取决于调用的函数版本)。因此变量 struct1
和 struct2
分别是指向类型 Struct1
和 Struct2
的指针。也不是指向类型 struct 的指针(无论如何我必须添加它不是真正的 Go 类型)。结构是一个关键字,有助于声明包含 fields/attributes.
根据您对其余代码的考虑 use-cases,可以改为尝试以下任一方法:
- 正如 mkopriva 所建议的那样,return 一个 interface{} 对象,但您需要使用类型断言来实际确定该对象
- 定义一个 Struct1 和 Struct2 都实现的接口,return一个指向这个的指针
- 制作可与 Struct1 或 Struct2 一起使用的单独函数。这并不一定像听起来那么糟糕,因为 Go 允许您以与传递类型相同的方式传递函数(参见
sort
包中的Less()
函数示例)。