在package initialization Go 规范的一部分,“词法文件名顺序”是什么意思?
To ensure reproducible initialization behavior, build systems are encouraged to present multiple files belonging to the same package in lexical file name order to a compiler.
最佳答案
来自 Wikipedia :
Lexical order is a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters.
在实践中,这意味着将文件名作为字符串进行比较,使用字符代码来决定顺序。英文字母的字符代码顺序遵循字母的自然顺序,但如果非字母也是文件名的一部分(例如数字和其他字符,如 '-'
).
如果包包含多个源文件,这只是定义源文件(任意)顺序的约定,如果重新编译包(当然文件不会重命名),该顺序将保持不变。
目的是让源文件始终以相同的顺序处理,因此包 init()
函数也将以相同的顺序执行,您将观察到相同的行为。包 init()
函数的顺序通常无关紧要,但也有可能是这样。通过遵循此词法文件名顺序约定,您可以依赖于 init()
函数的(固定)执行顺序。
关于go - 词汇文件名顺序是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31650965/