我有兩個包含不同資料的公共結構,以及一個包含兩個公共結構中的任何一個的私有中間結構。我還有一個函式可以解組中間結構,確定它包含哪個公共結構,并回傳兩個公共結構之一。
我面臨的問題是最后一個函式的回傳值。在最簡單的情況下,我認為我可以回傳*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
}
}
我知道有一種方法可以實作我想要實作的目標——我只是缺乏實作目標的經驗/理解。
感謝您的任何和所有輸入!
uj5u.com熱心網友回復:
您的unmarshallStruct函式回傳一個指向型別Struct1或的指標Struct2(取決于呼叫的函式的版本)。因此變數struct1和struct2分別是指向型別Struct1和的指標Struct2。也不是指向 struct 型別的指標(無論如何我必須添加它不是真正的 Go 型別)。結構是有助于宣告包含欄位/屬性的型別的關鍵字。
根據您為其余代碼考慮的用例,可以嘗試以下任何一種:
- 正如 mkopriva 建議的那樣,回傳一個 interface{} 物件,但您需要使用型別斷言來實際確保該物件
- 定義一個 Struct1 和 Struct2 都實作的介面,并回傳一個指向 this 的指標
- 制作適用于 Struct1 或 Struct2 的單獨函式。這并不像聽起來那么糟糕,因為 Go 允許您以與傳遞型別相同的方式傳遞函式(參見包中
Less()函式的示例sort)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/473048.html
標籤:去
上一篇:對匯入包進行單元測驗
下一篇:從另一個沒有重復的確定性int
