go run somefile.go and "+build ignore" directive

1,754 views
Skip to first unread message

Rémy Oudompheng

unread,
Feb 10, 2012, 4:19:02 PM2/10/12
to golang-nuts
Hello,

I have a little dilemma about the behaviour of the go tool when given
an explicit list of files. I am writing a package for which I want to
provide several single-file programs as examples.

Since there are also subpackages/commands, I'd like to be able to use
go install mypackage/... without it trying to compile together the
samples in mypackage/samples (where sample1.go, sample2.go etc. can be
found). Currently I have no other choice, I think, than either
renaming my samples directory "_samples".

So I tried adding // +build ignore at the top of the samples, but now
I can't do "go build sample1.go" or "go run sample1.go" anymore.
What are your suggestions?

Regards,
Rémy.

Kyle Lemons

unread,
Feb 10, 2012, 5:52:57 PM2/10/12
to Rémy Oudompheng, golang-nuts
I suggest using (a) the Example* functionality in your _test files or (b) moving them into their own goinstallable subdirectories along with their own tests and documentation.  The former is for lightweight demos, the latter is the right way in terms of go tool building.

Rémy Oudompheng

unread,
Feb 10, 2012, 6:30:56 PM2/10/12
to Kyle Lemons, golang-nuts
On 2012/2/10 Kyle Lemons <kev...@google.com> wrote:
> I suggest using (a) the Example* functionality in your _test files or (b)
> moving them into their own goinstallable subdirectories along with their own
> tests and documentation.  The former is for lightweight demos, the latter is
> the right way in terms of go tool building.

I precisely don't want them to be go-installable. They are samples,
not interesting programs. I would like them to be go-runnable (not as
packages but as single files), without them to be go-installable. And
my question is whether this requirement makes any sense to you or
anybody.

Rémy.

Andrew Gerrand

unread,
Feb 10, 2012, 6:58:47 PM2/10/12
to Rémy Oudompheng, golang-nuts, Kyle Lemons

Is it OK to ignore build directives for "go run"? Trying to think of a scenario where it'd be problematic. One argument is that anything complex enough to need build directives should really be a package in its own right.

Andrew

Kyle Lemons

unread,
Feb 11, 2012, 1:14:28 PM2/11/12
to Rémy Oudompheng, golang-nuts
Are they demonstrating something that's beyond the scope of the Example* functionality?  Perhaps I'm just thinking of the few times I've wanted this, and it's usually been to demonstrate parts of an API that I wrote.  In these cases, it's awesome to have them show up in godoc and to be compiled (and optionally executed and verified) automatically.

Russ Cox

unread,
Feb 18, 2012, 7:31:29 PM2/18/12
to Rémy Oudompheng, golang-nuts
On Fri, Feb 10, 2012 at 16:19, Rémy Oudompheng <remyoud...@gmail.com> wrote:
> What are your suggestions?

go run and go build now assume that if you pass a list of
files you know what you're doing. They ignore the +build lines.

Reply all
Reply to author
Forward
0 new messages