[daily][emacs][go] 配置emacs go-mode的编辑环境以及环境变量问题

1. 安装go 安装go-mode

  使用emacs编辑go代码的时候,你需要有正常可运行的go环境。

  并且有emacs的go-mode package

  https://www.emacswiki.org/emacs/GoLangMode

  https://github.com/dominikh/go-mode.el

1.1 安装索引工具

还要分别安装如下两个索引工具

go get -v golang.org/x/tools/cmd/guru
go get -v github.com/rogpeppe/godef

2. 环境变量引入

  除了1之外,正确运行的go环境还需要一个环境变量 GOPATH,一般我们会将GOPATH放在 ~/.bachrc或~/.bash_profile中。

  这个时候,在emacs中有一点比较特殊的情况。

2.1 

emacs在console中直接使用 emacs -nw来运行会自动继承环境变量,所以没有问题。

2.2

如果在x环境中启动emacs,它并不能引入环境变量。

这里有如下几个方法解决。

2.2.1 使用package exec-path-from-shell

  它的用途就是把bash的环境变量引进来,见:https://github.com/purcell/exec-path-from-shell

  但是我用的是fish,在~/.bashrc最下面有一行 exec fish。这会导致这个package报错,不做赘述。

2.2.2 使用自定义函数,如下:

(defun set-exec-path-from-shell-PATH ()
  (let ((path-from-shell (shell-command-to-string "/bin/bash -c 'echo $PATH'")))
    (setenv "PATH" path-from-shell)
    (setq exec-path (split-string path-from-shell path-separator))))

(defun set-exec-path-from-shell-GOPATH ()
  (let ((path-from-shell (shell-command-to-string "/bin/bash -c 'echo $GOPATH'")))
    (setenv "GOPATH" path-from-shell)
    (setq exec-path (split-string path-from-shell path-separator))))

;(set-exec-path-from-shell-PATH)
;(set-exec-path-from-shell-GOPATH)

  好用是好用的,但是我最终用来下面的方案

2.2.3 用~/.xprofile文件

x环境在启动时不会source ~/.bash_profile, 但是会source .xprofile. 所以,在他下面追加如下行

╰─>$ tail -n 2 ~/.xprofile
# add by tong 20190409
source ~/.bash_profile

并保证,所以环境变量,都写在/bash_profile里,其他bash交互相关的才写在~/.bashrc里,因为bashrc里有这样一行,使非交互场景绕开:

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

这样的话,emacs就天然有了环境变量,并且vitual stdio code也有了。

3 附一个有关go的emacs的配置,其中改了写快捷键,是go与c环境快捷键统一。

另外,要想各种跳转都好用,你的go工程能够编译通过也是十分重要的。

代码的跳转等,由go-mode支持,不在需要cscope了。不过全文搜索怎么实现还没配置。(

(add-hook 'go-mode-hook
      (lambda ()
        (fci-mode)
        (display-line-numbers-mode)
        (line-number-mode)
        (column-number-mode)
        (whitespace-mode)
        (local-set-key (kbd "C-c C-]") 'godef-jump)
(local-set-key (kbd "C-c C-r") 'go-guru-referrers) (local-set-key (kbd "C-c C-t") 'pop-tag-mark)))