Compile-time code execution: func build() #44633
Labels
FrozenDueToAge
WaitingForInfo
Issue is not actionable because of missing required information, which needs to be provided.
Summary
Embedding files into a Go executable with "embed" package is a strong feature implemented in version 1.16. In this issue, I propose some evolution of the concept to enable not only embedding files as possible now, but any type of arbitrary code execution at compile time.
Concept
Current capability to embed files is based on a compiler directive which "magically" populates a variable as below:
In this proposal I suggest embedding files would be executed from within a function named
func build()
. A comparison with "embed" package is belowThe
func build()
would operate similarly as existing withfunc init()
. But while init() is a run-time preliminary execution code, build() would be run at compile-time only and pre-populate global variables.A build function would enable not only to embed files, but also to apply calculations which would then populate global variables at compile time. Example of use case for such feature would be the calculation of CRC tables, among other big datasets which should be ready by compile time.
Benefits
func init()
;Risks / Impacts
This feature may conflict with existing code depending on package-level build() function, being used for different purposes.
The text was updated successfully, but these errors were encountered: