Index

I started a project in Go, when I got started everything was in a single file. Now this file is too big for my own taste, so I split it into 2 separate files, let’s call them main.go & util.go. In main.go I have the main() function, in util.go I have functions used by main.go.

When I tried to run main.go directly I got this error:

$ go run main.go
# command-line-arguments
./main.go:150: undefined: SomeFunction

I didn’t want to create a package just for util.go, sometime source files really are specific to a program and aren’t reusable.

My search for a solution on the web didn’t yield anything useful. I knew it was possible, I saw programs like godeb do it. After a while I built the program with go build to see if the error would be different, and it worked this time. Weird… What’s going on?

Everything was the same except I didn’t specify what to build, in that case main.go. Go just built every go source files in the directory, I got the same error with go build with only main.go specified.

$ go build main.go
# command-line-arguments
./main.go:150: undefined: SomeFunction

That’s when it hit me, I just needed to list all the files necessary to run the program on the command line:

$ go run main.go util.go

Here it is: go run needs the complete list of files to execute in the main package. I’ll know it for next time!