golang之vim和vscode开发环境


VIM配置

Ubuntu16下vim配置golang语言环境需要高版本VIM(8.0以上)。

1. 可直接源码编译安装最新版vim。

yum remove -y vim-enhanced

git clone https://github.com/vim/vim.git
git checkout -b v8.2.0340 v8.0.0340
make 
make install

2. 参考vim-go文档,通过vim包管理工具vim-plug安装vim-go。

curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
git clone https://github.com/fatih/vim-go.git ~/.vim/plugged/vim-go

在~/.vimrc中添加:

call plug#begin()
Plug 'fatih/vim-go', { 'do': ':GoInstallBinaries' }
call plug#end()

3. vim颜色配置。

默认安装后,可能出现错误后颜色会覆盖代码,导致不能阅读,可修改vim的colorscheme。

根据https://github.com/fatih/vim-go/wiki/Tutorial推荐使用https://github.com/fatih/molokai。

直接复制https://github.com/fatih/molokai/colors到~/.vim目录下。

并在~/.vimrc中添加:

let g:rehash256 = 1
let g:molokai_original = 1
colorscheme molokai

4. 其他配置。

~/.vimrc

call plug#begin()
Plug 'fatih/vim-go', { 'do': ':GoInstallBinaries' }
call plug#end()

let g:rehash256=1
let g:molokai_original=1
colorscheme molokai

set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set nu

let &termencoding=&encoding
set fileencodings=utf-8,gbk

map <F5> <ESC>:tp<CR>
map <F6> <ESC>:tn<CR>
$ tree ~/.vim -L 3
/home/wang/.vim
├── autoload
│   └── plug.vim
├── colors
│   └── molokai.vim
└── plugged
    ├── molokai
    │   ├── colors
    │   ├── LICENSE.md
    │   └── README.md
    └── vim-go
        ├── addon-info.json
        ├── assets
        ├── autoload
        ├── CHANGELOG.md
        ├── compiler
        ├── doc
        ├── Dockerfile
        ├── ftdetect
        ├── ftplugin
        ├── gosnippets
        ├── indent
        ├── LICENSE
        ├── Makefile
        ├── plugin
        ├── README.md
        ├── rplugin
        ├── scripts
        ├── syntax
        ├── templates
        └── test

5. 使用说明参考:https://github.com/fatih/vim-go/wiki/Tutorial

VS code配置 

Vscodego插件由微软官方退出, https://github.com/microsoft/vscode-goVscode开发go程序时,当编译第一个go程序时,vscode会提示安装多个go插件,建议选择Install All,之后如果对某个package效果不满意可以通过设置进行修改。

Go插件的相关配置(go.buildOnSavego.lintOnSave, go.testOnSave)setting中:setting -> Folder ->Extensions -> Go

此外可以通过extensions安装vim插件,实现vscode中vim编辑。

带参数调试

打开 .vscode 文件夹下的 launch.json 文件,找到 “args” 这一行,在 [ ] 中输入要传入的参数。

注:参数需要包括在双引号 “” 中,多个参数之间用逗号 , 分隔。

问题:

1. Gopls安装完成后提示”couldn’t start client gopls unsupported URI scheme: (gopls only supports file URIs)”

Gopls异常时代码提示和补全会异常,所以gopls要配置正确。

上述问题原因是vscode-go没有打开当前程序的目录,需要在文件夹中打开go程序文件。

 

参考:

1. https://gitee.com/yuxio/vim-go.git  https://github.com/fatih/vim-go/wiki/Tutorial

2. vim安装go插件vim-go和gocode,支持代码高亮、代码提示和语法检查等功能

3. vim如何配置go语言环境

4. VS code golang 开发环境搭建

5. vim 配置golang环境

6. vscode支持go https://geek-docs.com/vscode/vscode-tutorials/vscode-support-go.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM