Emacs golang 配置

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

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

在下载包之前需要设置好环境变量:

# Golang
export GOROOT=$HOME/go
export GOPATH=$HOME/development/go
export PATH=$PATH:$GOROOT/bin
export 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/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--

posted @ 2016-02-22 14:24  ibg  阅读(2238)  评论(0编辑  收藏  举报