duandao3265 2017-04-08 18:04
浏览 103
已采纳

Golang:可以肯定地说,如果一个结构体实现一个方法,那么它会满足所有定义该方法签名的接口?

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条回答 默认 最新

  • duanbushi1479 2017-04-08 18:44
    关注

    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.

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

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示