donglang1976 2019-03-24 10:37
浏览 80
已采纳

返回嵌套结构的接口

I'm trying to break circular dependencies in a reasonably sized golang project by using interfaces. I have some nested structs like this:

// these are in one package...
type config struct {
    region string
}

type system struct {
    name string
    config config
}

func (s system) getName() string {
    return s.name
}

func (s system) getConfig() config {
    return s.config
}

func (c config) getRegion() string {
    return c.region
}

And in another package that wants to use them I'm declaring some corresponding nested interfaces:

type iConfig interface {
    getRegion() string
}

type iSystem interface {
    getName() string
    getConfig() iConfig
}

// and has functions like
func New(system iSystem) {
    fmt.Printf("region=%s", system.getConfig().getRegion())
}

But when I try to use them like this:

theSystem := system{
    name: "testName",
    config:config{
        region:"testRegion",
    },
}

New(theSystem)      // doesn't work

I get an error:

cannot use theSystem (type system) as type iSystem in argument to New:
system does not implement iSystem (wrong type for getConfig method)
    have getConfig() config
    want getConfig() iConfig

It seems that because my concrete system struct returns a concrete type config Go doesn't think it satisifes the iConfig interface - even though I can use config via the iConfig interface directly. I was expecting config to implicitly satisfy the iConfig interface but that's not happening. How can I fix this?

Here's a link to the Go Playground.

  • 写回答

2条回答 默认 最新

  • drurhg37071 2019-03-24 13:41
    关注

    For your case, you may implement three packages for circular imports resolving:

    1. Config package

    Obtains a configuration for the application and provides it in a convenient way for its components.

    package config
    
    type IConfig interface {
        GetRegion() string
    }
    
    type config struct {
        region string
    }
    
    func New(region string) IConfig {
        return &config{
            region: region,
        }
    }
    
    func (c config) GetRegion() string {
        return c.region
    }
    
    
    1. System package

    Produces a system based on the configuration of your application.

    package system
    
    import (
        // "config"
    )
    
    type ISystem interface {
        GetName() string
        GetConfig() config.IConfig
    }
    
    type system struct {
        name string
        config config.IConfig
    }
    
    func New(name string, cfg config.IConfig) ISystem {
        return &system{
            name: name,
            config: cfg,
        }
    }
    
    func (s system) GetName() string {
        return s.name
    }
    
    func (s system) GetConfig() config.IConfig {
        return s.config
    }
    
    1. Third-party package

    Usage example:

    package main
    
    import (
        "fmt"
        // "config"
        // "system"
    )
    
    func UseConfig(cfg config.IConfig) {
        fmt.Printf("region=%s
    ", cfg.GetRegion())
    }
    
    func UseSystem(s system.ISystem) {
        fmt.Printf("region=%s
    ", s.GetConfig().GetRegion())
    }
    
    func main() {
        cfg := config.New("myregion")
        s := system.New("mysystem", cfg)
    
        UseConfig(cfg)
        UseSystem(s)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,