How to pass a go file name as arguments to go run

1,794 views
Skip to first unread message

bsr

unread,
Jan 5, 2017, 12:02:50 AM1/5/17
to golang-nuts
Following works.

go run cmd/main.go tests/a/in


flag.Parse()

args := flag.Args()
//args ->  []string{"tests/a/in"}

but when I do
 

go run cmd/main.go tests/a/in.go

named files must all be in one directory; have cmd/ and tests/a/


I am not trying to build both files, but "tests/a/in.go: is an argument to the built program at cmd/main.go


tried like


go run cmd/main.go "tests/a/in.go" , but same error.


thanks.

Dave Cheney

unread,
Jan 5, 2017, 12:07:55 AM1/5/17
to golang-nuts
 go run cmd/main.go -- tests/a/in.go

But you've probably reached the practical limit of what go run should be used for. I recommend using the expected GOPATH package layout, go build/install, etc.

bsr

unread,
Jan 5, 2017, 6:59:14 AM1/5/17
to golang-nuts
thanks Dave. This is to test a code generation utility during development, so not to build/install etc, but run directly. thanks again for your help.

Mandolyte

unread,
Jan 5, 2017, 9:45:02 PM1/5/17
to golang-nuts
Pass as a value to a named argument:
go run cmd/main.go -input tests/dir/a.go
Reply all
Reply to author
Forward
0 new messages