Dante
Dante

Reputation: 11264

Avoid typing out all go files in the main package when "Go run"-ng

When you have multiple .go files in a main package, i need to list them all out when doing a go run.

So when i have main.go, a.go, b.go and they all belong to the main package, i need to type go run main.go a.go b.go in order to use functions and structs inside the other 2 go files. The go build command however, is smart enough to link all files together automatically.

Have i misunderstood something about Go, or is this normal (listing out all the files in the main package when doing a go run)?

Upvotes: 1

Views: 908

Answers (2)

David Budworth
David Budworth

Reputation: 11626

If your source is in $GOPATH/src as in

$GOPATH/src/somedir/main.go
$GOPATH/src/somedir/a.go
$GOPATH/src/somedir/b.go

doing "go install somedir" will compile and install the somedir binary in to $GOPATH/bin

no need to list out the files individually

"go install" finds all the files on it's own.

My common work cycle with go is (from my bash prompt): $ clear; go install myproj && $GOPATH/bin/myproj

which, clears the screen, builds and installs "myproj" and then runs it

so, after I change code, I just hit Ctrl-C, Up arrow, and enter.

Entire cycle time is ~1 second (for small to start stuff)

Upvotes: 2

Eve Freeman
Eve Freeman

Reputation: 33145

The short answer is: you need to list them all out. You can do this with shell tricks, if you're desperate. I usually just write a shell script to go build and then run the resulting executable, after it gets too annoying.

# this works unless you have _test.go files
go run *.go

# if you really want to... enable extended glob
shopt -s extglob
# so you can do something like this (matches all .go except *_test.go files)
go run !(*_test).go

Check out this thread: https://groups.google.com/forum/#!topic/golang-nuts/EOi0VAQNA4c

Upvotes: 4

Related Questions