2

I write some code then build the app to an output file, but sometimes I just want to check if the app is buildable, i.e. has no errors and produces a compiled output, but without actually writing the output file.

I tried this variant and it seemed to work:

go build -o /dev/null myapp

But I suspect there must be a more "official" Go way to check if it can build.

Please advise!

Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335

4 Answers4

2

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() {
    // ...
}
Community
  • 1
  • 1
icza
  • 389,944
  • 63
  • 907
  • 827
1

How to check if a go app is buildable?

As I understand your question, you wanted to see if the file you're editing has no error.

then you can use vim-go plugin for vim.

And then setup your .vimrc for creating the shortcut :

"shortcut for vim-go
au FileType go nmap <leader>r <Plug>(go-run)
au FileType go nmap <leader>b <Plug>(go-build)
au FileType go nmap <leader>t <Plug>(go-test)
au FileType go nmap <leader>c <Plug>(go-coverage)

I use this as my daily work life when I want to see my file has no error I just type \+b. and then it will output the error without typing go build in the terminal.

hope it helps.

Gujarat Santana
  • 9,854
  • 17
  • 53
  • 75
0

One way would be gofmt -e my_file.go on all files, but:

  • that would not scale well
  • This doesn't actually report all errors that go build might.

Meaning: go build -o /dev/null might still be the more thorough approach.

For just validating syntax and structure: see gotype

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

I use VSCode and I've found it to be the better than Sublime when it comes to Go with this extension. When I save the document, it formats it well. It shows the the errors as I write code with the help of golint making development very manageable.

You can also use golint separately just like gofmt. You can also try go vet. There are debates going on around what to use when but I'd just advise to use both if you can.

Gaurav Ojha
  • 1,147
  • 1
  • 15
  • 37