Go build can't know with any degree of certainty if the destination file is a previously compiled version of the same program, therefore it has to recompile. Otherwise the following sequence would result in no work done, and no working executable despite compilation "success":
vi foo.go
rm -f foo
touch foo
go build -o foo foo.go # foo.go is older than foo, should we compile?
Go install doesn't take a "-o" option, because the name of the output file, and where it will be located is determined automatically from the package path and the package's type (library vs. binary). With this restriction in place, it can be determined with much greater confidence that the output file is the product of the given input, and so testing the timestamps to determine if compilation can be avoided makes sense.
I also would recommend against using a command like "go build source.go". It is not specifying a package, and as a result is much less useful (as well as requiring more typing).
In general, even when using go build, each executable should be in its own package / folder, even if its code is in a single file. The install location of the executable is determined from the package path automatically that way (provided it's in the GOPATH), and that name will be the same as the executable installed by "go install" or "go get" (ie: the name of the containing folder).
For example, if prog1 is defined in $GOPATH/src/prog1/main.go, then you can compile it using:
- "go build" if current directory is $GOPATH/src/prog1
- "go build prog1"
- or use "go install" versions instead to install the results (to $GOPATH/bin)
Important points:
- don't build go executables that way (you shouldn't be using -o except in special cases)
- your code arrangement is bad (each executable should be in it's own directory)
- dependencies are only rebuilt if they are out of date (even when using go build)
- "go build" will always rebuild the current package
- "go build" will throw away any dependencies it (re-)compiles, so out of date dependencies will be recompiled every time
Also, in case you didn't know, the "-v" flag to go install, build, get, and test all print out a line for each package where work was done. It prints out the package path when a package is (re-)compiled.
--
Carlos Castillo