how does go generate interact with build tags?

363 views
Skip to first unread message

Dan Kortschak

unread,
Feb 19, 2015, 5:40:21 PM2/19/15
to golan...@googlegroups.com
I have some example code (a package main with +build sometag) that is
generated. I would like to use go generate rather than have additional
instructions about which script in the directory was used to create the
example.

It would be nice to be able to `go generate -tags sometag` to only
generate the examples, however it seems that in the absence of a
specific file target go generate parses the entire directory without
examining tags. Is it likely that generate will become tag-aware or am I
missing something?

Rob Pike

unread,
Feb 19, 2015, 5:55:34 PM2/19/15
to Dan Kortschak, golan...@googlegroups.com
Go generate ignores build tags on purpose because it is not part of the build. I admit that if explicitly asked, it might make sense to parse them, but it needs some careful thought. Please feel free to file an issue.

-rob



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Viktor Kojouharov

unread,
Feb 19, 2015, 6:23:13 PM2/19/15
to golan...@googlegroups.com, dan.ko...@adelaide.edu.au
Does it? I'm on 1.4.1 and if the file containing the go:generate comment has a build tag, and I invoke `go generate` without giving it `-tag THE_TAG`, it won't do anything. It will only run the generate commands with the tag flag. Which is in fact what I was expecting and what I wanted, though it wasn't documented.

Dan Kortschak

unread,
Feb 19, 2015, 7:13:53 PM2/19/15
to Viktor Kojouharov, golan...@googlegroups.com
You are absolutely right. The bug is invalid.

thanks

Rangel Reale

unread,
Mar 15, 2023, 2:34:38 PM3/15/23
to golang-nuts
Sorry to revive this very old topic, but I didn't manage to make this work today, is it still supposed to work? ("go generate" using build tags).

Rangel Reale

unread,
Mar 15, 2023, 2:45:04 PM3/15/23
to golang-nuts
Forget it, it is working, I was using a build tag of "generate" which is special, and the only one for which it doesn't work.
Reply all
Reply to author
Forward
0 new messages