简体   繁体   中英

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

  • It is obvious that it is generated, so if it is missing (on build) the user then knows to run go generate

  • And I can then add the file to the .gitignore

My first guess is something like 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 ).

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.

So I think simply following the general guidelines for file names is enough, except maybe use _gen or _generated suffix. Or if your tool is public and used by others too, then use the name of the tool as the suffix (like stringer does).

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:

undefined: MustRunStringerGenerator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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