Emacs golang 配置

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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

  1. godoc
  2. godef
  3. gocode
  4. 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--

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.