duanemei2194 2018-03-02 20:53
浏览 37
已采纳

打包去耦

We all know dependency injection makes packages decoupled. But I'm a little confused about best practices of dependency injection in go.
Lets assume package User needs to access Config package.
We can pass a Config object to User methods. In this way I can change the Config package functionality as long as the new code resolves the interfaces. Another approach is call Config package methods directly , In these scenario I can change Config code too as long as the methods names remains the same. Like so

Update :

What is different between these two approaches :

package User

func foo(config ConfigObject) {
   config.Foo()
}

And this one :

package User

import Config

func foo() {
   config.Foo()
}
  • 写回答

1条回答 默认 最新

  • du4629 2018-03-04 12:57
    关注

    Calling config.Foo on the config argument to a method means that you receive an instance of some structure (possibly implementing interface Config) and call the method Foo on that instance/interface. Think of this as of calling a method of an object in OO terms:

    package user
    
    func foo(cfg config.Config) {
       cfg.Foo()
    }
    

    Calling config.Foo having imported the config package means you are calling the function Foo of package config, not of any object/struct/interface. Think of this as pure procedural programming without any objects:

    package user
    
    import config
    
    func foo() {
       config.Foo()
    }
    

    The latter has nothing to do with dependency injection, the former may constitute a part of it if Config is an interface.

    Dependency injection, on the other hand, follows generally the same rules in Go as in other languages:

    accept interfaces, supply implementations

    Because in Go structs satisfy interfaces implicitly rather than explicitly (as it is the case in Java)

    • the code accepting the value only needs to know about the interface and import it;
    • the code implementing it does not even need to know about the interface (it can just happen that it satisfies it);
    • the code that supplies the impl into a method accepting an interface, obviously, needs to know both.

    For your example this means:

    package config
    
    type Config interface {
        Foo() string
    }
    
    package foo
    
    type Foo struct{}
    
    func (f *Foo) Foo() string {
        return "foo"
    }
    
    package boo
    
    type Boo struct{}
    
    func (b *Boo) Foo() string {
        return "boo" 
    }
    
    package main
    
    func foo(cfg config.Config) string{
        return cfg.Foo()
    }
    
    func main() {
        // here you inject an instance of Foo into foo(Config)
    
        log.Print(foo(&foo.Foo{}))
    
        // here you inject an instance of Boo into foo(Config)
        log.Print(foo(&boo.Boo{})
    }
    

    Prints

    2018/03/03 13:32:12 foo

    2018/03/03 13:32:12 boo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据