To check if a package or app is buildable, go build
is the "official" way.
What you did is the easiest way. In my opinion, you should stick to it. Alternatively you may do:
go build -o delme && rm delme
But it's somewhat slower as it has to write the result which is then deleted, but this solution is platform independent (as /dev/null
does not exist on windows).
When building a command (main
package), by definition go build
will create and leave the result in the current working directory. If you build a "normal" package (non-main
), the results will be discarded. See details here: What does go build build?
So if it bothers you that you have to use the -o /dev/null
param or manually delete the result, you may "transform" your main
package to a non-main
, let's call it main2
. And add a new main
package which should do nothing else but import and call main2.Main()
. Building the main2
package will not leave any files behind.
E.g. myapp/main.go
:
package main
import "myapp/main2"
func main() { main2.Main() }
myapp/main2/main2.go
:
// Every content you originally had in main.go
package main2
func Main() {
// ...
}