Golang: Is it safe to say that if a struct implements a method then it satisfies all interfaces that define that method's signature?

huangapple go评论88阅读模式
英文:

Golang: Is it safe to say that if a struct implements a method then it satisfies all interfaces that define that method's signature?

问题

在Docker源代码库中,存在一个在image/backend.go中的接口:

type imageBackend interface {
    ....
	ImagesPrune(pruneFilters filters.Args) (*types.ImagesPruneReport, error)
}

而在daemon/prune.go中有一个实现:

func (daemon *Daemon) ImagesPrune(pruneFilters filters.Args) (*types.ImagesPruneReport, error) {
    ... 实现细节 ...
}

这是否意味着可以说Daemon实现了imageBackend接口?

背景:
我试图理解如何调用docker system prune命令会调用daemon.go中的ImagesPrune函数。我可以追踪代码流程如下:

> cli/../system/prune.go > -> cli/../prune/prune.go > -> cli/../image/prune.go > -> client/image_prune.go > -> api/server/..image/image_routes.go > -> api/server/../image/backend.go > -----> ??? ----> daemon/prune.go

我不知道上面的???部分是什么。

英文:

In the docker source repo,
there exists an interface in image/backend.go:

type imageBackend interface {
    ....
	ImagesPrune(pruneFilters filters.Args) (*types.ImagesPruneReport, error)
}

and, there is an implementation in daemon/prune.go:

func (daemon *Daemon) ImagesPrune(pruneFilters filters.Args) (*types.ImagesPruneReport, error) {
    ... implementation details ...
}

Does this mean it's correct to say that Daemon implements the imageBackend interface?

Background:
I'm trying to understand how calling docker system prune cmd invokes the ImagesPrune function in daemon.go. I could trace the code flow as:

> cli/../system/prune.go
> -> cli/../prune/prune.go
> -> cli/../image/prune.go
> -> client/image_prune.go
> -> api/server/..image/image_routes.go
> -> api/server/../image/backend.go
> -----> ??? ----> daemon/prune.go

I don't know what comes in the ??? section above.

答案1

得分: 4

是的,Daemon确实实现了imageBackend接口(正如评论中指出的那样,实际上是*Daemon类型实现了该接口)。imageBackend的所有方法都在daemon包内的各个源代码文件中实现(主要是image_*.go文件)。

image_routes.go中调用了postImagesPrune方法,该方法又调用了s.backendImagesPrune方法。s是指向imageRouter实例的指针。

type imageRouter struct {
    backend Backend
    decoder httputils.ContainerDecoder
    routes  []router.Route
}

cmd/dockerd/daemon.go这里,使用Daemon的实例初始化了imageRouterbackend

因此,当调用s.backend.ImagesPrune时,实际上是运行了Docker Daemon的ImagesPrune方法,正如你在上面指出的那样,该方法位于daemon/prune.go中。

英文:

Yes, Daemon does implement the imageBackend interface (as pointed out in the comments, it's actually the *Daemon type that implements the interface). All of imageBackend's methods are implemented in various source code files inside the daemon package (mostly the image_*.go ones).

In the image_routes.go the postImagesPrune method is called, which in turn calls the ImagesPrune method of s.backend. s is a pointer to the instance of imageRouter.

type imageRouter struct {
    backend Backend
    decoder httputils.ContainerDecoder
    routes  []router.Route
}

This imageRouter instance is initialized with backend set to an instance of Daemon in cmd/dockerd/daemon.go here.

So when s.backend.ImagesPrune is called, it is running the ImagesPrune method of the Docker Daemon, which as you point out above is in daemon/prune.go.

huangapple
  • 本文由 发表于 2017年4月9日 02:04:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/43298575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定