這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
在配置前需要下載用到的包:
- godoc
- godef
- gocode
- oracle
在下載包之前需要設定好環境變數:
# Golangexport GOROOT=$HOME/goexport GOPATH=$HOME/development/goexport PATH=$PATH:$GOROOT/binexport PATH=$PATH:$GOPATH/bin
如果網路良好的話使用這種方法:
godoc:
go get golang.org/x/tools/cmd/godoc
這樣會將godoc二進位檔案安裝到$GOROOT/bin目錄裡。
godef:
go get github.com/rogpeppe/godef
這樣會將godef二進位檔案安裝到$GOPATH/bin目錄裡。
gocode 自動完成:
go get -u github.com/nsf/gocode
這樣會將gocode二進位檔案安裝到$GOPATH/bin目錄裡。
go oracle
go get golang.org/x/tools/cmd/oracle
oracle二進位檔案將出現在$GOPATH/bin目錄裡,將它移動到$GOROOT/bin目錄裡。
下面是emacs的golang配置:
;;; 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目錄裡!
~/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/gotourgolang.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/tourgit 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--