3

Here is how my project is structure

├── cmd
│   ├── orders
│   │   ├── main.go
└── scripts
    └── codegenerator.go

the codegenerator.go file is the file where i have put my code to generate the code. Here is the logic for codegenerator.go

  • rename main.go to old_main.go
  • read from old_main.go line by line and write to a new file called main.go
  • insert new lines/code blocks based on markers/placeholder in main.go
  • remove old_main.go file

The go:generate directive is in main.go like this

//go:generate go run ../../scripts/codegenerator.go

I ran go generate from command line and wanted to pass in 3 arguments so that they can be utilized in codegenerator.go. here is my command and the errors i got (i executed this command on path cmd/orders)

 $ go generate arg_one arg_two arg_three
can't load package: package arg_one: unknown import path "arg_one": cannot find module providing package arg_one
can't load package: package arg_two: unknown import path "arg_two": cannot find module providing package arg_two
can't load package: package arg_three: unknown import path "arg_three": cannot find module providing package arg_three

So the questions are

  1. Am i running go generate properly?
  2. How can i pass in arguments from go generate command line to the target script
Em Ae
  • 8,167
  • 27
  • 95
  • 162
  • 1
    The arguments are specified in the `//go:generate` comment. If you need to specify the arguments on the command line, then it might be more appropriate to run the command directly than to use `go generate`. – Charlie Tumahai Oct 30 '19 at 02:24
  • @CeriseLimón so if the arguments changes for every code generation the go:generate isn't a good option? – Em Ae Oct 30 '19 at 02:41
  • 1
    The `go generate` command is not a good option in this scenario. What problem are you trying to solve by using `go generate` vs. running the command directly? – Charlie Tumahai Oct 30 '19 at 02:51
  • Whenever we introduce a new table in our system we have to write some boiler plate code in our existing files. That code is mainly a copy paste except that based on table name new variables are created. – Em Ae Oct 30 '19 at 02:57
  • 1
    OK, but what is the reason for using `go generate` instead of running the command directly? What is the problem you are trying to solve. – Charlie Tumahai Oct 30 '19 at 02:58
  • Was just going through `go generate` and thought I can use this to solve the problem. Currently I am using `make` file. I read through docs but couldn't find the solution so I posted here. I guess I have to stay with `make` file then. – Em Ae Oct 30 '19 at 03:00
  • 1
    If this only needs to run when you add a table, `go generate` seems like the wrong tool entirely; this should be a stand-alone executable. `go generate` is used for re-generatable code; e.g. anything where you'd pull down the repo and run an initial build sequence of `go get;go generate;go build` to reproduce the generated files then build the project. – Adrian Oct 30 '19 at 14:08

1 Answers1

5

While I agree you may be better off using another solution, there isn't anything preventing usage of env vars (os specifics may vary)

➜  /tmp cat foo.go

package main

//go:generate python run.py $ARG

func main() {}
➜  /tmp cat run.py
import sys

print sys.argv[1]
➜  /tmp ARG=poop go generate foo.go
poop
sberry
  • 128,281
  • 18
  • 138
  • 165