go - 具有动态类型的结构

标签 go struct interface

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

我想将对象保存在数组/slice 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)

我该如何解决这个问题?

最佳答案

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

您可以将 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})
}

关于go - 具有动态类型的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48659218/

相关文章:

json - 嵌套结构的解码字段不起作用

http - 获取现有的 gorilla session

c - 结构的编译定义在 C 中是什么样子的?

C++:头文件中结构的 vector

c# - 分解神对象的接口(interface)继承?

go - 为什么隐式非指针方法不满足接口(interface)?

c - 结构变量用 printf 改变值

java - Google Go 中抽象类/方法 (Java) 的等价性

c++ - 如何将类型为抽象类的变量转换为其子类?

go - 终止第二个 goroutine