Emacs golang 配置

在配置前須要下載用到的包:html

  1. godoc
  2. godef
  3. gocode
  4. oracle

在下載包以前須要設置好環境變量:git

# Golang
export GOROOT=$HOME/go
export GOPATH=$HOME/development/go
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/bin

 

若是網絡良好的話使用這種方法:
godoc:github

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

這樣會將godoc二進制文件安裝到$GOROOT/bin目錄裏。

godef:golang

go get github.com/rogpeppe/godef 

這樣會將godef二進制文件安裝到$GOPATH/bin目錄裏。

gocode 自動完成:web

go get -u github.com/nsf/gocode

這樣會將gocode二進制文件安裝到$GOPATH/bin目錄裏。

go oracleshell

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

oracle二進制文件將出如今$GOPATH/bin目錄裏,將它移動到$GOROOT/bin目錄裏。

瀏覽器

下面是emacs的golang配置:bash

;;; init-go --- golang
;;; Commentary:
;; http://tleyden.github.io/blog/2014/05/22/configure-emacs-as-a-go-editor-from-scratch/
;; https://robinxiong.gitbooks.io/golang/content/section1/emacs.html
;; http://studygolang.com/topics/583

;;; Code:

(require-package 'go-mode)
(require-package 'company-go)

(require 'go-mode)

;; removes all unused imports
(add-hook 'go-mode-hook '(lambda() (local-set-key (kbd "C-c C-r")'go-remove-unused-imports)))

;; format the current buffer
(add-hook 'go-mode-hook '(lambda () (local-set-key (kbd "C-c C-f") 'gofmt)))

;; format the buffer when save
(add-hook 'before-save-hook 'gofmt-before-save)

;; show the go documentation for a given package
;; Note: godoc depends on the godoc utility.
;; It must be installed and on your $PATH.
;; To install it run: go get code.google.com/p/go.tools/cmd/godoc.
(add-hook 'go-mode-hook '(lambda() (local-set-key (kbd "C-c C-k") 'godoc)))

;; Gocode autocomplete
;;(add-hook 'go-mode-hook 'company-mode)
(add-hook 'go-mode-hook '(lambda()
               (set (make-local-variable 'company-backends)'(company-go))
               (company-mode)))

;; Go oracle
;; Note: $GOPATH will defined in init-exec-path-from-shell
(load-file "$GOPATH/src/golang.org/x/tools/cmd/oracle/oracle.el")
(add-hook 'go-mode-hook 'go-oracle-mode)

(provide 'init-go)
;;; init-go.el ends here

 

;;; init-exec-path-from-shell --- exec path form shell
;;; Commentary:
;; Let Emacs use .bashrc file,especially system $PATH.
;;; Code:

(require-package 'exec-path-from-shell)

(when (memq window-system '(mac ns x))
(exec-path-from-shell-initialize))

;;; for golang
(exec-path-from-shell-copy-env "GOPATH")

(provide 'init-exec-path-from-shell)
;;; init-exec-path-from-shell.el ends here

另外一種方法:
注意:從github克隆的golang.org應該放在src目錄裏!websocket

~/development/go/src  ᐅ git clone https://github.com/golang/tools golang.org/x/tools
正克隆到 'golang.org/x/tools'...
remote: Counting objects: 15398, done.
接收對象中:   8% (1232/15398), 404.01 KiB | 69.00 KiB/s   

編譯godoc:網絡

go build golang.org/x/tools/cmd/godoc

注意:編譯出的godoc二進制文件應該放在 ~/development/go/bin目錄裏!

安裝golang教程(這個是英文版的):

git clone https://github.com/golang/tour
go build golang.org/x/tour/gotour
golang.org/x/tools/playground/socket/socket.go:37:2: cannot find package "golang.org/x/net/websocket" in any of:
    /home/z/go/src/golang.org/x/net/websocket (from $GOROOT)
    /home/z/development/go/src/golang.org/x/net/websocket (from $GOPATH)

怎麼辦?

ᐅ git clone https://github.com/golang/net

注意:gotour和net這2個目錄和tools目錄是平級的,它們都在$GOPATH/src/golang.org/x 目錄下。
x
├── net
├── tools
└── tour
ᐅ go build golang.org/x/tour/gotour

安裝中文版的教程:

git clone https://github.com/Go-zh/tour github.com/Go-zh/tour
git clone https://github.com/Go-zh/tools github.com/Go-zh/tools

注意tour和tools是同級目錄。

github.com/Go-zh
├── tools
└── tour

編譯中文版教程:

go build github.com/Go-zh/tour/gotour 

這時會在$GOPATH/src目錄中出現一個gotour二進制文件,把它剪切到$GOPATH/bin目錄中並重命名爲gotour-zh。
在$GOPATH/bin中執行:./gotour-zh 便可開啓瀏覽器。

安裝godef:

git clone https://github.com/rogpeppe/godef github.com/rogpeppe/godef
go build github.com/rogpeppe/godef

github.com/rogpeppe/godef/acme.go:11:2: cannot find package "9fans.net/go/acme" in any of:
    /home/z/go/src/9fans.net/go/acme (from $GOROOT)
    /home/z/development/go/src/9fans.net/go/acme (from $GOPATH)

解決方法:

git clone https://github.com/9fans/go 9fans.net/go 

而後再編譯安裝godef:

go build github.com/rogpeppe/godef

參考:

http://studygolang.com/topics/583
http://tleyden.github.io/blog/2014/05/22/configure-emacs-as-a-go-editor-from-scratch/ https://robinxiong.gitbooks.io/golang/content/section1/emacs.html

 

--End--

相關文章
相關標籤/搜索