Skip to content

Instantly share code, notes, and snippets.

@samertm
Last active March 31, 2019 16:21
Show Gist options
  • Save samertm/8bccfeb30c0902194de5 to your computer and use it in GitHub Desktop.
Save samertm/8bccfeb30c0902194de5 to your computer and use it in GitHub Desktop.
Tricking Out Emacs for Go
;; from https://www.youtube.com/watch?v=r6j2W5DZRtA
;; get the following packages ("M-x package-list-packages"):
;; go-mode
;; go-eldoc
;; company-mode
;; company-go
;; get the following go programs (run each line in your shell):
;; go get golang.org/x/tools/cmd/godoc
;; go get golang.org/x/tools/cmd/goimports
;; go get github.com/rogpeppe/godef
;; go get github.com/nsf/gocode
(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(setq company-idle-delay nil)
(setq gofmt-command "goimports")
;; UPDATE: gofmt-before-save is more convenient then having a command
;; for running gofmt manually. In practice, you want to
;; gofmt/goimports every time you save anyways.
(add-hook 'before-save-hook 'gofmt-before-save)
(global-set-key (kbd "C-c M-n") 'company-complete)
(global-set-key (kbd "C-c C-n") 'company-complete)
(defun my-go-mode-hook ()
;; UPDATE: I commented the next line out because it isn't needed
;; with the gofmt-before-save hook above.
;; (local-set-key (kbd "C-c m") 'gofmt)
(local-set-key (kbd "M-.") 'godef-jump))
(set (make-local-variable 'company-backends) '(company-go)))
(add-hook 'go-mode-hook 'my-go-mode-hook)
(add-hook 'go-mode-hook 'go-eldoc-setup)
(add-hook 'go-mode-hook 'company-mode)
@djahma
Copy link

djahma commented Aug 18, 2014

Hi, doesn't your setup misses a "(require 'package)" at the beginning?
Otherwise, kool setup! You got me started for Go in Emacs;-)

@samertm
Copy link
Author

samertm commented Sep 13, 2014

Yup! Thanks for catching that!

@ambarishmalpani
Copy link

Wow! Thank you so much!

@madwind76
Copy link

Thank you...

@EvanMisshula
Copy link

FYI the installation command for goimports command has changed:

 go get golang.org/x/tools/cmd/goimports

and the new command for installing godef can be found here

 go get github.com/rogpeppe/godef

http://stackoverflow.com/questions/31867347/i-am-having-trouble-getting-golang-org-x-tools-cmd-goimports
http://stackoverflow.com/questions/35401625/where-to-get-godef-binary:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment