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 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题