Skip to content

Instantly share code, notes, and snippets.

@yudai
Created March 20, 2015 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yudai/8ac50af71dccd3fd390c to your computer and use it in GitHub Desktop.
Save yudai/8ac50af71dccd3fd390c to your computer and use it in GitHub Desktop.
To fix go-remove-unused-imports
(defun my-go-unused-imports-lines ()
;; FIXME Technically, -o /dev/null fails in quite some cases (on
;; Windows, when compiling from within GOPATH). Practically,
;; however, it has the same end result: There won't be a
;; compiled binary/archive, and we'll get our import errors when
;; there are any.
(reverse (remove nil
(mapcar
(lambda (line)
(when (string-match "^\\(.+\\):\\([[:digit:]]+\\): imported and not used: \".+\".*$" line)
(let ((error-file-name (match-string 1 line))
(error-line-num (match-string 2 line)))
(if (string= (file-truename error-file-name) (file-truename buffer-file-name))
(string-to-number error-line-num)))))
(split-string (shell-command-to-string
(concat go-command
(if (string-match "_test\.go$" buffer-file-truename)
" test -c"
" build -o /dev/null"))) "\n")))))
(advice-add 'go-unused-imports-lines :override #'my-go-unused-imports-lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment