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--

时间: 2024-07-30 09:55:28

Emacs golang 配置的相关文章

Emacs安装配置全攻略之一编译安装简单配置

/******************************************************************************************************************************************/ 原创作品,转载时请务必以超链接形式标明文章原始出处:http://blog.csdn.net/gqb_driver/article/details/29407717,作者:gqb666 /***************

Viper--方便好用的Golang 配置库

前言 本文主要是为读者介绍一个轻便好用的Golang配置库viper 正文 viper 的功能 ? viper 支持以下功能: ? 1. 支持Yaml.Json. TOML.HCL 等格式的配置 ? 2. 可以从文件.io.环境变量.command line中提取配置 ? 3. 支持自动转换的类型解析 ? 4. 可以远程从etcd中读取配置 示例代码 定义一个类型: type config struct { v *viper.Viper; } 用于测试的Yaml配置文件 TimeStamp: "

CentOS6.x之emacs安装配置编译

刚开始学习linux,干学没什么意思,想在linux下写写程序,了解到linux下使用较多的是emacs和vim,在youtobe上分别看了看这两个工具进行开发的视频,个人感觉emacs比较酷一点,所以选择了emacs. 闲话少说,下面写一下自己安装.配置.编译c++程序并运行的过程. 1.安装emacs 刚开始我选择了下载代码安装,make的时候总是出错,找不到解决方案. 后来直接yum -y -install emacs安装了.(安装之前要把emacs用到的一些依赖库安装好,建议使用root

Ubuntu linux下部署golang配置环境,极客学院 无闻讲的安装配置是错的,折腾我好几遍,真是有点坑

开始按极客学院无闻讲的做,弄了几遍都不行,最后发现是错的,别人告诉我这是mac下的virtualbox是这样的,不管怎样,被坑的不浅. 虽然sudo apt install golang-go 就能安装,但是不是最新版本.安装最新版本的正确步骤如下: 一.需要文件 golang:http://www.golangtc.com/download 二.安装golang 一)从上文给出的链接中选择所需要的版本的golang包(选linux-amd64.tar.gz版本,本人是64位系统,我安装时最新版

ubuntu14.04 下emacs 24 配置

目的: 配置emacs 24 适合编程开发 主要参考JerryZhang的配置(Emacs 简易教程) http://www.perfect-is-shit.com/emacs-simple-tutorial.html#tocAnchor-1-16-5 https://gitcafe.com/JerryZhang/Emacs-Config 优点: 插件少 基本功能都比较全,emacs 配置后启动比较快速 安装过程如下: git clone https://gitcafe.com/JerryZha

重新载入emacs的配置!

不重启 Emacs 让新修改的配置内容生效,有四个函数可以做到: M-x eval-last-sexp 使当前 elisp 配置中光标前的那一条语句立刻生效: M-x eval-region 使当前 elisp 配置中选中的 region 中的语句立刻生效: M-x eval-buffer 使当前的 buffer 中的设置语句立刻生效: M-x load-file ~/.emacs 载入 .emacs 文件,从而使其中的设置生效,要生效其它 elisp 文件只需要把 .emacs 文件名换成其它

如何不重新启动 Emacs 就让 .emacs 的配置起作用

老是重新启动 emacs ,效率很低,其实可以不用重启Emacs就可以让新的配置生效 用 emacs 打开 .emacs 文件,C-x C-e 光标前面的运行一条语句.立即生效. 选择一个 region , M-x eval-region M-x load-file ~/.emacs M-x eval-buffer

Emacs考场配置

当年\(NOip\)考场配置不记得啦 存在这里搞事情 (global-set-key [f9] 'compile) (global-set-key [f10] 'gud-gdb) (global-set-key (kbd "C-s") 'save-buffer) (global-set-key (kbd "C-a") 'mark-whole-buffer) (global-set-key (kbd "RET") 'newline-and-inde

golang配置

配置使用yaml,使用了github上一个configor的库.理由如下: 1. 支持多种格式 2. ORM,自动给变量赋值,不用写太多的代码 3. 但是他支持shell env配置,我怕与运行的环境变量受影响,所以把这个功能去掉.在修改的过程中,意识到测试驱动开发的重要性,因为我在重构修改代码的时候,发现 go test -v 能够显示多个测试用例运行的情况.在看测试用例的过程中,发现了库的使用方法和应用场景.单单看文档不足以明白,同时防止修改代码影响了其他功能.