Lunx vimgo 开发环境搭建


本文介绍 Liunx 下 vim-go 的开发环境搭建。主要参考这篇博客进行的配置,其中记录了几个搭建环境时遇到的问题。

1. vim-go 开发环境搭建

1.1 用户隔离

由于使用的是共享宿主机,为不影响其它用户使用,首先新建用户:

# 新建用户 chunqiu
useradd chunqiu
passwd chunqiu

# visudo 更改 sudoers 使得 chunqiu 能提权到 root
chunqiu ALL = ALL NOPASSWD: ALL

切换身份到 chunqiu, go get github 查看能否下载 github 包。发现下载不了,查看 go env 发现 GOPROXY 模式为 direct,配置国内代理:

[chunqiu@coolBoy-wksp-a cobra]$ go env -w GOPROXY="https://goproxy.cn,direct"
[chunqiu@coolBoy-wksp-a cobra]$ go env | grep proxy
GOPROXY="https://goproxy.cn,direct"

除了代理项外,检查 GO111MODULE 配置:

[chunqiu@coolBoy-wksp-a cobra]$ go env | grep 111
GO111MODULE="on"

注意这里 GO111MODULE 有三种配置,off/on/auto 他们分别表示:

  • GO111MODULE = on: 强制使用 Go 模块,需要 go.mod 才能工作。
  • GO111MODULE = off: 强制 Go 使用 GOPATH 模式,即使在 GOPATH 外。
  • GO111MODULE = auto: Go 1.13 版本之后的行为是,当存在 go.mod 或处于 GOPATH 外,其行为等同于 GO111MODULE = on;当存在 GOPATH 内,且没有 go.mod 文件存在时,其行为等同于 GO111MODULE = off。

注:详细了解 GO111MODULE 可参看这里
再次 go get github 包发现 download 成功。

1.2 搭建 vim-go

根据引言的博客搭建 vim-go,具体搭建流程不介绍了,博客介绍的够详细了。这里主要记录下搭建过程中遇到的问题。

1.2.1 HTTP/2 stream1

下载插件时报错,提示:HTTP/2 stream 1 was not closed cleanly before end of the underlying stream。

在 root 用户下,下载插件没问题,在 chunqiu 用户下下载出错。发现是 git 默认的通信协议出问题,将默认的通信协议修改为 http/1.1:

git config --global http.version HTTP/1.1

再次下载插件,发现报错信息提示: Failed to connect to github.com port 443:connection timed out。

发现在公司网络下 git 未设置代理,设置代理:

git config --global http.proxy http://10.***.*.**:8080
git config --global https.proxy https://10.***.*.**:8080

再次下载插件提示成功。

1.2.2 Tagbar: Exuberant ctags not found!

下载插件报错,提示

Tagbar: Exuberant ctags not found!
Please download Exuberant Ctags from ctags.sourceforge.net and install it in a directory in your $PATH or set g:tagbar_ctags_bin.

下载 ctags:

# yum
sudo yum install ctags-etags

# apt-get
sudo apt-get install exuberant-ctags

重新下载插件 ctags 成功。

1.2.3 ycmd server SHUT DOWN

下载插件成功,vim 提示:The ycmd server SHUT DOWN (restart with ':YcmRestartSer... using it. Follow the instructions in the documentation.

出现这样的报错可能是 bundle/vim-plug 更新了 YCM(YouCompleteMe) , 而本地没有重新编译 YCM。YCM 提示重新编译,重新编译如下:

[chunqiu@coolBoy-wksp-a YouCompleteMe]$ ls ~/.vim/plugged/YouCompleteMe/
autoload     CODE_OF_CONDUCT.md  COPYING.txt  install.py  plugin          python     run_tests.py  third_party  vimrc_ycm_minimal
codecov.yml  CONTRIBUTING.md     doc          install.sh  print_todos.sh  README.md  test          tox.ini
[chunqiu@coolBoy-wksp-a YouCompleteMe]$ ./install.py

编译提示 ERROR: Unable to find executable ‘cmake’. CMake is required to build ycmd, 缺少 cmake,centos 下 yum install cmake 安装 cmake,重新编译,成功。

1.2.4 cannot determine module path

go run 报错:

go: cannot determine module path for source directory ... (outside GOPATH, module path must be specified)

解决方式是需要创建 module:

  1. 在 src 目录下创建项目:

    go mod init <project_name>
    
  2. go mod tidy,下载依赖包。

再次编译,运行 go run 成功。

芝兰生于空谷,不以无人而不芳。
原文地址:https://www.cnblogs.com/xingzheanan/p/15543654.html