【问题标题】:Struct with dynamic type具有动态类型的结构
【发布时间】:2018-02-07 08:36:19
【问题描述】:

我现在正在学习 Go,我编写了一个小项目,其中包含一些向内部日志报告的探针。我有一个基本探针,我想创建新的探针来扩展基本探针。

我想将对象保存在数组/切片 LoadedProbes 中。

type LoadableProbe struct {
    Name   string
    Probe  Probe
    Active bool
}

var LoadableProbes []LoadableProbe

基本的探测结构是:

type ProbeModule struct {
    version   VersionStruct
    name      string
    author    string
    log       []internals.ProbeLog
    lastcall  time.Time
    active    bool
}

func (m *ProbeModule) New(name string, jconf JsonConfig) {
    // read jsonConfig 
}
func (m *ProbeModule) Exec() bool {
    // do some stuff
    return true
}

func (m *ProbeModule) String() string {
    return m.name + " from " + m.author
}

func (m *ProbeModule) GetLogCount() int {
    return len(m.log)
}
[...]

我正在将此基本结构用于其他探针,例如:

type ShellProbe struct {
    ProbeModule
}

func (s *ShellProbe) New(name string, jconf JsonConfig) {
    s.ProbeModule.New(name, jconf)
    fmt.Println("Hello from the shell")
}

func (s *ShellProbe) Exec() bool {
    // do other stuff
    return true
}

在 Init() 期间,我调用以下代码:

func init() {
    RegisterProbe("ShellProbe", ShellProbe{}, true)
}

func RegisterProbe(name string, probe Probe, state bool) {
    LoadableProbes = append(LoadableProbes, LoadableProbe{name, probe, state})
}

现在的问题是我无法将 Shellprobe 类型添加到 LoadableProbe 结构,它需要一个 Probe 结构。

我的想法是使用 interface{} 代替 Loadable Probe 结构中的 Probe 结构。但是当我调用 Probe 对象的 New() 方法时:

for _, p := range probes.LoadableProbes {
    probe.Probe.New(probe.Name, jconf)
}

但我得到了错误:p.Probe.New undefined (type interface {} is interface with no methods)

我该如何解决这个问题?

【问题讨论】:

  • 您没有分享您对 Probe 类型的定义。如果您添加它,答案可能会更具体。
  • “我想创建新的探针来扩展基本探针” - 停在那里。你不能在 Go 中“扩展”; Go 没有继承。如果你这样想,你将创建一个与 Go 根本不兼容的设计;您会发现自己以不寻常的方式滥用构图并造成无法维护的混乱。在尝试像这样的复杂设计之前,我强烈建议您阅读 Go 接口、它们的工作原理以及它们的使用方式。

标签: go struct interface


【解决方案1】:

如果您在每种探针类型中都有公共数据字段,您可以考虑使用Probe 作为定义基本数据字段和基本方法的具体基本类型,并使用新的ProbeInterface 接口作为抽象基本类型定义常见的预期方法签名,允许您传递/收集/管理不同的专用探针类型。

您可以将Probe 嵌入到每个专门的探针类型中,并且将根据嵌入规则提升方法和字段。看起来您对嵌入很熟悉,但如果您最近没有看过它,那么细节值得在the "Embedding" section of Effective Go 中查看。

您可以覆盖专用探针类型中的 Probe 方法以执行特定于类型的代码。

它可能看起来像这样:

type ProbeInterface interface {
     New()
     Exec() bool
     // whatever other methods are common to all probes
}

type Probe struct {
    // whatever's in a probe
}

func (p *Probe) New() {
    // init stuff
}

func (p *Probe) Exec() bool {
    // exec stuff
    return true
}

// Probe's methods and fields are promoted to ShellProbe according to the rules of embedding
type ShellProbe struct {
    Probe
    // any specialized ShellProbe fields
}

// override Probe's Exec() method to have it do something ShellProbe specific.
func (sp *ShellProbe) Exec() bool {
    // do something ShellProbe-ish
    return true
}

type LoadableProbe struct {
    Name        string
    P           ProbeInterface
    Active      bool
}

func RegisterProbe(name string, probe ProbeInterface, state bool) {
    LoadableProbes = append(LoadableProbes, LoadableProbe{name, probe, state})
}

【讨论】:

    【解决方案2】:

    您的问题有不同的方法。

    最直接的答案是:在调用任何方法之前,您需要将 interface{} 转换为具体类型。示例:

    probe.Probe.(ShellProbe).New(...)
    

    但这是一个真正令人困惑的 API。

    更好的方法可能是重新考虑您的整个 API。您提供的信息有限,很难进行这种级别的设计思考。

    我不知道这是否适合你,但一个常见的模式是定义一个接口:

    type Probe interface {
        New(string, JsonConfig)
        Exec() bool
        // ... etc
    }
    

    然后让所有探针类型实现接口。然后使用该接口而不是 interface{},就像您最初所做的那样:

    type LoadableProbe struct {
        Name   string
        Probe  Probe
        Active bool
    }
    

    那么你的语法应该会再次起作用,因为Probe 接口包含一个New 方法。

    probe.Probe.New(...)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 2017-02-07
      • 2019-02-02
      • 1970-01-01
      相关资源
      最近更新 更多