英文:
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.backend
的ImagesPrune
方法。s
是指向imageRouter
实例的指针。
type imageRouter struct {
backend Backend
decoder httputils.ContainerDecoder
routes []router.Route
}
在cmd/dockerd/daemon.go
的这里,使用Daemon
的实例初始化了imageRouter
的backend
。
因此,当调用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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论