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配置
Vscode的go插件由微軟官方退出, https://github.com/microsoft/vscode-go。Vscode開發go程序時,當編譯第一個go程序時,vscode會提示安裝多個go插件,建議選擇Install All,之后如果對某個package效果不滿意可以通過設置進行修改。
Go插件的相關配置(go.buildOnSave, go.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,支持代碼高亮、代碼提示和語法檢查等功能
6. vscode支持go https://geek-docs.com/vscode/vscode-tutorials/vscode-support-go.html