简体   繁体   English

生成的go文件的名称

[英]Name for a generated go file

I'm generating a Go file (to include constants such as build version etc) so that the constants can be used in other packages. 我正在生成一个Go文件(包括诸如构建版本之类的常量等),以便常量可以在其他包中使用。 I have a created a small tool that will create the file with go generate but am trying to think of an appropriate name so that 我有一个创建了一个小工具,它将使用go generate创建文件,但我正在尝试考虑一个合适的名称

  • It is obvious that it is generated, so if it is missing (on build) the user then knows to run go generate 很明显它是生成的,所以如果它缺失(在构建时),则用户知道运行go generate

  • And I can then add the file to the .gitignore 然后我可以将文件添加到.gitignore

My first guess is something like version_GENERATED.go 我的第一个猜测是像version_GENERATED.go

Any conventions I should be aware of or better suggestions? 我应该注意的任何约定或更好的建议?

Having a suffix like _GENERATED added to the file name does not hold any information until the file is generated, as the compiler will just give you "unrelated" errors like "undefined: xxx" (the compiler won't guess that if the identifier would exists, it would be in version_GENERATED.go ). 在生成文件之前,在文件名中加上像_GENERATED这样的后缀不会保留任何信息,因为编译器只会给你“无关”错误,如"undefined: xxx" (编译器不会猜测,如果标识符会存在,它将在version_GENERATED.go )。

For example the stringer generator generates files with name type_string.go where type is replaced with the name of the type it is generated for. 例如, stringer发生器产生具有名称的文件type_string.go其中类型被替换为它是为生成的类型的名称。

So I think simply following the general guidelines for file names is enough, except maybe use _gen or _generated suffix. 所以我认为仅仅遵循文件名的一般准则就足够了,除非使用_gen_generated后缀。 Or if your tool is public and used by others too, then use the name of the tool as the suffix (like stringer does). 或者,如果您的工具是公开的并且也被其他人使用,则使用工具的名称作为后缀(如stringer一样)。

If you do want the user to get a talkative error message in case your generator is yet to be run, your generator may generate an exported constant whose name is talkative if included in an error message, like: 如果您确实希望用户在生成器尚未运行时收到健谈错误消息,则生成器可能会生成一个导出的常量,如果包含在错误消息中,该常量的名称是健谈的,例如:

const MustRunStringerGenerator = 0

And in your program refer to it like: 在你的程序中,请参考它:

var _ = MustRunStringerGenerator // Test if stringer has been run

If stringer has not yet been run, you'll see an error message: 如果stringer尚未运行,您将看到一条错误消息:

undefined: MustRunStringerGenerator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM