在源代码中,Docker的注册驱动程序是在哪里初始化的?

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

Where are docker's registered drivers being initialized in the source code?

问题

我正在尝试理解Docker的源代码,同时也在学习Go语言。Docker中一个让我困惑的地方是在docker/daemon/graphdriver/driver.go的第75行,通过访问strings映射的name属性来获取类型为InitFunc的函数。然而,我找不到drivers在哪里被初始化为注册的驱动程序,似乎需要在某个地方进行初始化。

英文:

I'm trying to understand Docker's source code while also learning Go. One area of Docker that has me confused is inside of docker/daemon/graphdriver/driver.go on line 75 where the name property of the strings map is accessed to obtain the function with type InitFunc. However, I can't seem to find where drivers is initialized with the registered drives, and it seems that needs to be done somewhere.

答案1

得分: 2

init()函数的第62行(链接)中:

func init() {
    drivers = make(map[string]InitFunc)
}

然后在每个依赖该函数的包中,它们调用graphdriver.Register,例如:

func init() {
    graphdriver.Register("btrfs", Init)
}

关于init()函数,来自于http://golang.org/doc/effective_go.html:

每个源文件可以定义自己的无参数init函数来设置所需的任何状态。(实际上,每个文件可以有多个init函数。)而且,init函数在包中所有变量声明的初始化器都已经求值之后被调用,而这些初始化器只有在所有导入的包都已经初始化之后才会被求值。

除了无法表示为声明的初始化之外,init函数的常见用途是在真正执行开始之前验证或修复程序状态的正确性。

英文:

In line 62 inside init():

func init() {
	drivers = make(map[string]InitFunc)
}

And then in each package that depends on it they call graphdriver.Register, for example:

func init() {
	graphdriver.Register("btrfs", Init)
}

//edit

From http://golang.org/doc/effective_go.html about init():

> Each source file can define its own niladic init function to set up
> whatever state is required. (Actually each file can have multiple init
> functions.) And finally means finally: init is called after all the
> variable declarations in the package have evaluated their
> initializers
, and those are evaluated only after all the imported
> packages have been initialized.
>
> Besides initializations that cannot be expressed as declarations, a
> common use of init functions is to verify or repair correctness of the
> program state before real execution begins.

huangapple
  • 本文由 发表于 2014年7月19日 07:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/24835301.html
匿名

发表评论

匿名网友

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

确定