英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论