How do I set flycheck-compile to run go-build on save?
I can't find documentation on how to run flycheck-compile with go-build on save.
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityThe basic mechanism you're looking for is to add a hook; if you aren't familiar, hooks are an emacs mechanism that causes a specific function to be called at a particular time. For instance, go-mode-hook
will call a set of functions every time go-mode
starts.
Now: there is an after-save-hook
. The thing is, it's global. So you could do this, in your .emacs.d
/init.el
/config file of choice:
(add-hook 'after-save-hook (lambda () (flycheck-compile 'go-build)))
But this will cause flycheck-compile
to run any time you save anything.
I can see two options:
flycheck
has a go-build
syntax checker, which you can select and just use. If you don't need the extra trimmings of compile-mode
, this will get you highlighted errors, etc. You can set the checker using a .dir-locals.el
. go-mode
.Option two looks like this:
(add-hook 'after-save-hook
(lambda ()
(if (eq major-mode 'go-mode)
(flycheck-compile 'go-build))))
This introduces the minor cost of checking for go-mode every time you save, but that could be no bother to you. You could define functions to add and remove that hook, if you wanted, but then you'd have to remember to enable/disable this behavior, which doesn't seem like what you want. (That's a very short step from simply calling flycheck-compile
yourself, so.)