Golang 访问公共结构的数据元素

Golang Access Data Elements of Common Struct

我将有一个应用程序,其中包含许多遵循 Req/Rsp 设计的消息。下面实现的是消息 Foo 的单个 Req/Rsp。 Req 将遵循用于处理消息和 return 响应的通用接口。所有 Rsp 消息都将嵌入 MHRSP 结构。我想在 ProcessRequest 方法之外设置响应状态。我遇到的问题是由下面标记为 Problem Area

的代码块导致的运行时错误

panic: interface conversion: interface {} is *main.FooRsp, not main.MHRSP

因为我会有很多消息,而不仅仅是 FooRsp,转换为 FooRsp 不是解决方案,所以问题是,我如何以通用方式处理这个问题(即能够为任何 Rsp 设置成功和错误消息)?

使用 GDB Online 编译的代码:https://www.onlinegdb.com/online_go_compiler

/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
package main
import (
    "fmt"
    "errors"
    "encoding/json"
)

type MHREQIF interface {
    ProcessRequest(e int) (interface{}, error)
}

type MHRSP struct {
    Success bool `json:"success,omitempty"`
    ErrorMessage string `json:"error_message,omitempty"`
}

type FooReq struct {
    MHREQIF
    Name string `json:"name"`
}

type FooRsp struct {
    MHRSP
    Name string `json:"name"`
}

func (self *FooReq) ProcessRequest(e int) (interface{}, error) {
    rsp := FooRsp{Name:self.Name}
    if 0 == e {
        return &rsp, nil    
    } else {
        return &rsp, errors.New("crap")
    }
}

func main() {
    var msg_req MHREQIF
    msg_req = &FooReq{Name:"bob"}
    rsp, err := msg_req.ProcessRequest(1)
    if err != nil {
        // -------------- PROBLEM AREA BEG ------------------
        v := rsp.(MHRSP)
        v.Success = false
        v.ErrorMessage = fmt.Sprintf("%v", err)
        // -------------- PROBLEM AREA END ------------------
    }
    msg_bytes, _ := json.Marshal(rsp)
    msg_string := string(msg_bytes)
    fmt.Println(msg_string)
}

感谢@mkopriva 和@Adrian 为我指明了正确的方向。有时,从 C++ OO 到 go-isms 的转变是令人费解的:P

这里是工作代码,适用于在如何解决此类问题上有类似困难的任何人。

/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
https://www.onlinegdb.com/online_c++_compiler
*******************************************************************************/
package main
import (
    "fmt"
    "errors"
    "encoding/json"
)

type MHREQIF interface {
    ProcessRequest(e int) (MHRESPIF, error)
}

type MHRESPIF interface {
    SetResponse(err error)
}

type MHRSP struct {
    MHRESPIF `json:"-"`
    Success bool `json:"success"`
    ErrorMessage string `json:"error_message,omitempty"`
}

type FooReq struct {
    MHREQIF
    Name string `json:"name"`
}

type FooRsp struct {
    MHRSP
    Name string `json:"name"`
}

func (self *MHRSP) SetResponse(err error) {
    if err != nil {
        self.Success = false
        self.ErrorMessage = fmt.Sprintf("%v", err)
    } else {
        self.Success = true
    }
}

func (self *FooReq) ProcessRequest(e int) (MHRESPIF, error) {
    rsp := FooRsp{Name:self.Name}
    if 0 == e {
        return &rsp, nil    
    } else {
        return &rsp, errors.New("crap")
    }
}

func main() {
    var msg_req MHREQIF
    msg_req = &FooReq{Name:"bob"}
    rsp, err := msg_req.ProcessRequest(1)
    rsp.SetResponse(err)
    msg_bytes, _ := json.Marshal(rsp)
    msg_string := string(msg_bytes)
    fmt.Println(msg_string)
}