词汇文件名顺序是什么意思?

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

What does lexical file name order mean?

问题

在Go语言规范的包初始化部分中,"lexical file name order"是什么意思?

为了确保可复现的初始化行为,构建系统鼓励将同一个包中的多个文件按照词法文件名顺序呈现给编译器。

英文:

In the package initialization part of the Go specification, what does "lexical file name order" mean?

> 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.

答案1

得分: 11

Wikipedia

词汇顺序是对单词按字母顺序排列的方式的一种概括,该方式基于单词组成字母的字母顺序。

实际上,这意味着文件名被作为字符串进行比较,使用字符编码来确定顺序。英文字母的字符编码顺序遵循字母的自然顺序,但如果非字母也是文件名的一部分(例如数字和其他字符如'-'),字符编码顺序就很重要了。

这只是一种约定,用于定义包含多个源文件的包的(任意的)源文件顺序,如果重新编译包(当然文件没有被重命名),该顺序将保持不变。

其目的是确保源文件始终按照相同的顺序进行处理,因此包的init()函数也将按照相同的顺序执行,并且您将观察到相同的行为。通常情况下,包的init()函数的顺序并不重要,但也可能存在某些情况下它是重要的。通过遵循这种词汇文件名顺序约定,您可以依赖于init()函数的(固定的)执行顺序。

英文:

From Wikipedia:

> Lexical order is a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters.

In practice this means files names are compared as strings, using the character codes to decide order. Order of character codes of the English alphabet follow the natural order of the letters, but the character code order is important if non-letters are also part of the file name (e.g. digits and other characters like '-').

This is just a convention to define an (arbitrary) order of source files if a package contains multiple source files, an order which remains the same if the package is recompiled (and of course files are not renamed).

The purpose is so that source files are always processed in the same order, and therefore the package init() functions will also be executed in the same order, and you will observe the same behavior. Often the order of package init() functions does not matter, but there may be cases when it does. By following this lexical file name order convention, you can rely on the (fixed) execution order of the init() functions.

huangapple
  • 本文由 发表于 2015年7月27日 18:40:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/31650965.html
匿名

发表评论

匿名网友

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

确定