wire
wire copied to clipboard
Missing `go run` or `go build` example of generated code
https://github.com/google/wire/blob/79660af94317b9c466adc39f27f469f1079c779b/_tutorial/README.md?plain=1#L115
I think this step is a missing example of how to run
or build
the generated code. Also, it's not obvious that we need also to include the generated files when executing either go run
or go build
like go build main.go wire_gen.go
.
I think adding an extra paragraph explaining how to run or build would be very useful & prevent frustration, especially for people new to go.
When following the steps, I think most people will set seeing the word Hi there!
on the console to be the goal to verify that we get the same outcome as in the non-wire code.
Yeah, I think there is something missing in the instructions as well. If I do go run main.go
, I get...
# command-line-arguments
./main.go:77:12: undefined: InitializeEvent
doing go build
first then it runs fine when using the built executable.
@troyatomic you need to execute the wire
command to generate the bindings.
https://github.com/google/wire/blob/main/_tutorial/README.md#using-wire-to-generate-code

I agree it would be good to add some documentation about this. I'm fairly new to Go, and this was not intuitive to me. Up till now, I've been running my project with go run cmd/myproject/myproject.go
. However, after integrating with Wire and generating wire_gen.go
, I get something like this when I run that command:
# command-line-arguments
cmd\myproject\myproject.go:16:21: undefined: InitializeMyType
The same thing happens with go build cmd/myproject/myproject.go
. Being fairly new to Go, I didn't realize I could (or needed to) pass in multiple go files to go run
or go build
. I eventually figured out I could run it by switching to my project's cmd/myproject
directory and then using go run .
.
After that, I tried to run it from my project root directory using go run cmd/myproject/
, but I was confused when I got the following error:
package cmd/myproject is not in GOROOT
Finally, I figured out I need to put ./
in front of the project directory to tell Go I'm trying to run that instead of an installed package: go run ./cmd/myproject/
.
Anyway, all this is to say the documentation @cglotr is suggesting would have saved me some time and frustration.
Whoops, didn't notice till just now that @cglotr has a pull request out for this. 😅 Thanks, @cglotr!