by lobster_johnson · 8y ago · 0 comments
Right now, we have the magical //go:generate comment, but since it requires manual invocation of "go generate", artifacts can and do get out of sync with the Go code.
It would much nicer if Go code could contain code that caused generation to happen when the code was compiled. Adding the ability to execute Go at compile time would be one possible solution, similar to Rust's custom derive [1] support. As a use case, Rust has libraries that make extensive use of this system, such as serde [2], which can automatically generate efficient, type-safe serialization/deserialization code for structs (e.g. to/from JSON).
[1] https://doc.rust-lang.org/book/first-edition/procedural-macr...
It's pretty easy to enforce that as a build constraint in your CI system; we do that with our Go projects. If the output of `go generate` isn't checked into git, the build fails. (Same if the code hasn't been formatted with gofmt).