Give a name to the binary using go build

25,035 views
Skip to first unread message

Srinath G S

unread,
Jul 8, 2013, 4:43:38 AM7/8/13
to golan...@googlegroups.com
HI all,

This must be a simple issue. But have been trying to figure out since long.

Here is what I tried to do.

there is a file called wiki.go in a folder.

I tried to compile using go build command in two ways

1) go build wiki.go 
    this creates a binary named wiki in the same folder. 

I wanted the output to go in different folder so, I tried this

2) go build wiki.go -o build/wiki
but here, it throws the following error: named files must be .go files. 


Am I missing something here?

Thanks and regards,

Srinath

minux

unread,
Jul 8, 2013, 4:45:25 AM7/8/13
to Srinath G S, golan...@googlegroups.com
On Mon, Jul 8, 2013 at 4:43 PM, Srinath G S <g.s.sr...@gmail.com> wrote:
This must be a simple issue. But have been trying to figure out since long.

Here is what I tried to do.

there is a file called wiki.go in a folder.

I tried to compile using go build command in two ways

1) go build wiki.go 
    this creates a binary named wiki in the same folder. 

I wanted the output to go in different folder so, I tried this

2) go build wiki.go -o build/wiki
but here, it throws the following error: named files must be .go files. 
go build -o build/wiki wiki.go 

Srinath G S

unread,
Jul 8, 2013, 4:49:25 AM7/8/13
to golan...@googlegroups.com
Aah, Thanks :-)

yaprak....@gmail.com

unread,
Aug 15, 2014, 4:35:53 AM8/15/14
to golan...@googlegroups.com, g.s.sr...@gmail.com
I was facing the same problem and this really worked. 
Thanks Minux.

However, I do not understand the reason. Why do we have to write the output path first?

8 Temmuz 2013 Pazartesi 11:45:25 UTC+3 tarihinde minux yazdı:

Carlos Castillo

unread,
Aug 15, 2014, 11:41:52 AM8/15/14
to golan...@googlegroups.com, g.s.sr...@gmail.com, yaprak....@gmail.com
On Friday, August 15, 2014 1:35:53 AM UTC-7, yaprak....@gmail.com wrote:
I was facing the same problem and this really worked. 
Thanks Minux.

However, I do not understand the reason. Why do we have to write the output path first?

To keep the flag package simple, the first non-flag is treated as the first command line argument, regardless of what might come after, so all flags must come before all regular arguments.

The go tool uses the flag package and so is subjected to the same limitations for it's built-in subcommands. Therefore the "output" flag must be set before the arguments (which contain the input).

example:
go build foo -o bar -> flags: {} args: [foo, -o, bar]
go build -o bar foo -> flags: {o: bar} args: [foo] 

Yaprak Ayazoglu

unread,
Aug 15, 2014, 11:50:44 AM8/15/14
to Carlos Castillo, golan...@googlegroups.com, g.s.sr...@gmail.com
Crystal clear!

Thanks.
--
Yaprak Ayazoglu

Reply all
Reply to author
Forward
0 new messages