Emacs and Go

With this stuff in ~/.emacs, you should get the following features:
  • (not really a feature) a strange delay the first time you open a Go file, while a bunch of dev tools get installed into GOPATH
  • With point on an identifier, M-. jumps to the identifier's definition (M-* returns to original point)
  • Gofmt and goimports get applied automatically when saving
  • Requires gocode goimports and godef
(setq-default indent-tabs-mode nil)

(require 'package)
(add-to-list 'package-archives
             '("melpa" . ""))
(add-to-list 'package-archives
             '("melpa-stable" . "") t)
(when (< emacs-major-version 24)
  ;; compatibility libraries like cl-lib                                                                                                                                                                                        
  (add-to-list 'package-archives '("gnu" . "")))
(add-to-list 'package-archives
             '("melpa" . "") t)

(define-key global-map (kbd "RET") 'newline-and-indent)

(setq package-list '(async auto-complete dash
  go-add-tags go-autocomplete go-errcheck go-mode go-playground
  go-rename gotest markdown-mode
(dolist (package package-list)
  (unless (package-installed-p package)
    (message "installing %s..." package)
    (package-install package)))

(defun arvados-gopath ()
  (let ((bfn (buffer-file-name)))
    (setenv "GOPATH" 
              (call-process "bash" nil t nil "-c" 
                            "gp=${GOPATH:-${HOME}/go}; d=${0}; while [[ ${d} != '' ]]; do if [[ -e ${d}/services/keep-web/. ]]; then gp=${d}/tmp/GOPATH; mkdir -p ${gp}/src/; ln -sfn ${d} ${gp}/src/; break; fi; d=${d%/*}; done; mkdir -p ${gp}; echo -n ${gp}" 
              (buffer-substring (point-min) (point-max))))))

(defun go-mode-omnibus ()
  (let ((gopath (arvados-gopath)))
    (make-local-variable 'process-environment)
    (make-local-variable 'exec-path)
    (add-to-list 'exec-path (concat gopath "/bin"))
      (call-process "bash" nil t t "-c" "([[ -x ${1}/bin/gocode ]] && ${1}/bin/goimports -srcdir / /dev/null) || go get -u -v" "-" gopath))
    (auto-complete-mode 1)
                                        ; call gofmt before saving                                                                                                                                                              
    (add-hook 'before-save-hook 'gofmt-before-save)
    (setq gofmt-command "goimports")
                                        ; compile with go                                                                                                                                                                       
    (if (not (string-match "go" compile-command))
        (set (make-local-variable 'compile-command)
             "go build -v && go test -v && go vet"))
                                        ; godef-jump key bindings                                                                                                                                                               
    (local-set-key (kbd "M-.") 'godef-jump)
    (local-set-key (kbd "M-*") 'pop-tag-mark)))
(add-hook 'go-mode-hook 'go-mode-omnibus)

Updated by Ward Vandewege almost 2 years ago · 4 revisions