0

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.

1

The 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:

  1. Stop trying to do this at all. 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.
  2. Make your hook function check for 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.)

  • ty! this is what I needed. I wasn't sure how to go about it. – RNikoopour May 1 '18 at 3:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.