词汇文件名称顺序是什么意思? [英] What does lexical file name order mean?

查看:150
本文介绍了词汇文件名称顺序是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Go规范的程序包初始化部分中,词法文件名顺序为了确保可重复的初始化行为,鼓励构建系统
以呈现属于$ b中相同包的多个文件$ b词法文件名顺序到编译器。


解决方案

From Wikipedia


词汇顺序为一般来说,单词的字母顺序是基于字母顺序的字母顺序。

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



如果一个包包含多个源文件,这只是一个定义源文件(任意)顺序的约定,如果重新编译包(当然文件没有重新命名),它们仍然是相同的。

目的是让源文件总是以相同的顺序处理,因此包 init()函数也将以相同的顺序执行,并且您将观察到相同的行为。程序包 init()函数的顺序通常无关紧要,但可能会出现这种情况。按照这个词法文件名顺序约定,您可以依赖 init()函数的(固定)执行顺序。


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.

解决方案

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.

这篇关于词汇文件名称顺序是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆