Emacs go mode


Youtube 影片

Emacs Go Mode

  • Go mode
    • 程式美化 Integrate with gofmt
      • goimports 取代 gofmt
    • 函數定義 Integrate with godoc
    • Integrate with godef, jump-to definition
    • Integrate with guru, jump-to caller, jump-to-callee…
    • 語法檢查 flycheck: golint
    • 程式補全 Code completion: company with gocode
    • 函數定義 Eldoc
      • go-eldoc with gocode


# for go setup
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
  • 建立目錄(workspace)
    • bin
    • src
      • github.com
        • yourgithubname

go 安裝

$go get golang.org/x/tools/cmd/goimports
$go get golang.org/x/tools/cmd/godoc
$go get github.com/rogpeppe/godef
$go get golang.org/x/tools/cmd/guru
$go get golang.org/x/lint/golint
$go get github.com/nsf/gocode
$go get golang.org/x/tools/cmd/gorename

Emacs 安裝

(use-package go-mode
  :ensure t
  :mode (("\\.go\\'" . go-mode))
  :hook ((before-save . gofmt-before-save))
  (setq gofmt-command "goimports")
  (use-package company-go
    :ensure t
    (add-hook 'go-mode-hook (lambda()
                              (add-to-list (make-local-variable 'company-backends)
                                           '(company-go company-files company-yasnippet company-capf company-dabbrev)))))
  (use-package go-eldoc
    :ensure t
    :hook (go-mode . go-eldoc-setup)
  (use-package go-guru
    :ensure t
    (add-hook 'go-mode-hook #'go-guru-hl-identifier-mode)

讓 company-go 認得自己的 package

$gocode set autobuild true



package myutil

// Sum : sum of tow int
func Sum(num1, num2 int) int {
	return num1 + num2

// Multiply : two int multiplication
func Multiply(num1, num2 int) int {
	return num1 * num2


package main

import (


func main() {
	fmt.Println("Hello World")
	fmt.Printf("%d\n", myutil.Sum(1, 32))
	fmt.Printf("%d\n", myutil.Multiply(3, 6))