“生成”多行命令

I'm trying to use //go:generate to run an external tool before compiling my code, and as I need to pass a certain number of parameters, the line becomes rather long.

It seems that there is no way to write a multiline go:generate command, is it correct? Are there alternative approaches?

Thanks

There is no way to split go generate command into several lines, but there are some tips.

If you need to run multiple short commands you can write them one by one like the following.

//go:generate echo command A
//go:generate echo command B
//go:generate ls

You also should know that there is not a bash script but a raw command. So the following works not as one may expect.

//go:generate echo something | tr a-z A-Z > into_file
// result in "something | tr a-z A-Z > into_file"

For long or complex commands you should use separate script (or maybe go program) that is called from go:generate comment.

//go:generate generate.sh
//go:generate go run generator.go arg-A arg-B

In generator.go you should use build tag to prevent it from normal compilation with other files.

// +build ignore

package main
// ...

The best place to learn go is go sources: https://github.com/golang/go/blob/master/src/runtime/runtime.go#L13