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))
  :config
  (setq gofmt-command "goimports")
  (use-package company-go
    :ensure t
    :config
    (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
    :config
    (add-hook 'go-mode-hook #'go-guru-hl-identifier-mode)
    )
  )

讓 company-go 認得自己的 package

$gocode set autobuild true

簡單測試程式

github.com/xxx/myproject/myutil/main.go

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
}

github.com/xxx/myproject/main.go

package main

import (
	"fmt"

	"github.com/jerryhsieh/myproject/myutil"
)

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

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *