-gcflags option on compiler, passing in multiple flags

979 views
Skip to first unread message

dtne...@gmail.com

unread,
Mar 26, 2014, 11:28:48 PM3/26/14
to golan...@googlegroups.com
Hi all,

I'm a new developer in Go. I am a grad student in computational science and I'm working on creating a library for local search in Go. I know that if I want to pass in a flag such as "race" to the compiler, i can run my Go program with "go build -gcflags '-race' myprogram.go". But I am trying to figure out how to pass in multiple flags. I looked at the compiler code and couldn't find it. I'll search more, but i figure that someone here is likely to know the quick answer.

The reason I am doing this is because I modified the compiler source code so that I can disable errors from unused variables, which makes developing much faster for me. I understand why unused variables are a bad idea, so I only want this as a compiler option, therefore my need for passing in flags. So for example, I'll run my program with "go build -gcflags '-unused_vars_ok' myprogram.go", and it won't give me errors for unused variables. However, I'd like to extend this so that I can also ignore unused imported packages, but first i need to figure out how to pass in multiple flags :)

Dan Kortschak

unread,
Mar 26, 2014, 11:55:08 PM3/26/14
to dtne...@gmail.com, golan...@googlegroups.com
Have you tried using goimports and Jan Merkl's technique of defining a
use func in your tests suite so you can safely ignore non-use during
testing?

in foo_test.go:

func use(_ ...interface{}) {}

then in foo.go:

func warble() {
var bar, baz, shmoo int

// vars I'm messing with right now:
use(bar, baz, shmoo)

// stuff I'm breaking.
}

This will allow bar, baz and shmoo to be unused during testing, but the
package will fail to compile for a build - as it should.

minux

unread,
Mar 26, 2014, 11:57:51 PM3/26/14
to dtne...@gmail.com, golang-nuts


On Mar 26, 2014 11:45 PM, <dtne...@gmail.com> wrote:
>
> Hi all,
>
> I'm a new developer in Go. I am a grad student in computational science and I'm working on creating a library for local search in Go. I know that if I want to pass in a flag such as "race" to the compiler, i can run my Go program with "go build -gcflags '-race' myprogram.go". But I am trying to figure out how to pass in multiple flags. I looked at the compiler code and couldn't find it. I'll search more, but i figure that someone here is likely to know the quick answer.

-gcflags "-l -m"


> The reason I am doing this is because I modified the compiler source code so that I can disable errors from unused variables, which makes developing much faster for me. I understand why unused variables are a bad idea, so I only want this as a compiler option, therefore my need for passing in flags. So for example, I'll run my program with "go build -gcflags '-unused_vars_ok' myprogram.go", and it won't give me errors for unused variables. However, I'd like to extend this so that I can also ignore unused imported packages, but first i need to figure out how to pass in multiple flags :)

this is highly discouraged. I'd go so far as to say that disable that feature means you're not using Go.

Dan Kortschak

unread,
Mar 27, 2014, 12:15:34 AM3/27/14
to golan...@googlegroups.com
On Thu, 2014-03-27 at 14:25 +1030, Dan Kortschak wrote:
> Merkl

s/Merkl/Mercl/

Sorry, Jan.


Daniel Newman

unread,
Mar 27, 2014, 12:18:20 AM3/27/14
to minux, golang-nuts
minux, I certainly understand your point of view and I definitely can see why this might no longer be considered "Go" since it goes against the language's specs (albeit with an option flag, not with the default, which I plan to use when finalizing my code). I'm very new to Go and I certainly am not at the point yet where I can make judgments about the various aspects of the language. But as an educational exercise, I am having fun and learning a lot by looking through and making changes to the compiler. I appreciate the responses to my question so far.

As to my original question, if anyone knows how to pass in multiple flags, that would still be useful for me to know! Thanks!

Dan Kortschak

unread,
Mar 27, 2014, 12:21:21 AM3/27/14
to Daniel Newman, minux, golang-nuts
On Thu, 2014-03-27 at 00:18 -0400, Daniel Newman wrote:
> As to my original question, if anyone knows how to pass in multiple
> flags, that would still be useful for me to know! Thanks!

minux answered that: e.g. -gcflags "-l -m" (space separated list of
flags).

Daniel Newman

unread,
Mar 27, 2014, 12:23:19 AM3/27/14
to Dan Kortschak, minux, golang-nuts
Didn't see... That's easy. Thank you!
Reply all
Reply to author
Forward
0 new messages