当前位置:   article > 正文

Vim打造成GoLang开发环境_gofumpt vim

gofumpt vim

码字不易,转载请附原链,搬砖繁忙回复不及时见谅,技术交流请加QQ群:909211071

源码安装GoLang

进入:Downloads - The Go Programming Language

下载:wget https://go.dev/dl/go1.19.2.linux-amd64.tar.gz

安装: tar -xzf go1.16.darwin-amd64.tar.gz -C /usr/local/

配置环境变量:vim ~/.bash_profile

  1. export GOROOT=/usr/local/go
  2. export GOPATH=/Users/why/Desktop/go
  3. export PATH=$PATH/bin:$GOROOT/bin:$GOPATH/bin
  4. alias gosrc="cd $GOROOT/src"
  5. alias gopath="cd $GOPATH"
  6. alias gomod="cd $GOMOD"
  7. alias gorm='gormt -g=true'
  8. alias gotest='gotests -all -w ./'
  9. alias goformat='gofmt -l -w -s ./'
  10. alias goimport='goimports -l -w ./'
  11. alias gorace='go run -race .'
  12. alias cover="go test -coverprofile=cover.out"
  13. alias covers="go tool cover -html=cover.out"

配置go env:

  1. go env -w GO111MODULE=on
  2. go env -w GOPROXY=https://goproxy.cn,direct
  3. go env -w GOROOT=/usr/local/go
  4. go env -w GOBIN=/usr/local/go/bin
  5. go env -w GOPATH=/Users/why/go

安装Go相关工具

这里只列出来一部分,剩下的按需安装

  1. go install github.com/google/gops@latest
  2. go install github.com/mdempsky/gocode@latest
  3. go install golang.org/x/tools/gopls@latest
  4. go install github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest
  5. go install github.com/ramya-rao-a/go-outline@latest
  6. go install github.com/acroca/go-symbols@latest
  7. go install golang.org/x/tools/cmd/guru@latest
  8. go install golang.org/x/tools/cmd/gorename@latest
  9. go install github.com/go-delve/delve/cmd/dlv@latest
  10. go install github.com/rogpeppe/godef@latest
  11. go install github.com/sqs/goreturns@latest
  12. go install golang.org/x/lint/golint@latest
  13. go get -u -v github.com/cweill/gotests/...
  14. go install github.com/fatih/gomodifytags@latest
  15. go install github.com/josharian/impl@latest
  16. go install github.com/davidrjenni/reftools/cmd/fillstruct@latest
  17. go install github.com/haya14busa/goplay/cmd/goplay@latest
  18. go install github.com/godoctor/godoctor@latest
  19. go install github.com/smartystreets/goconvey@latest
  20. go install github.com/jstemmer/gotags@latest
  21. go install golang.org/x/tools/cmd/goimports@latest
  22. go install golang.org/x/tools/cmd/godoc@latest
  23. go install github.com/xxjwxc/gormt@latest(gormt -g=true
  24. go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
  25. go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
  26. go install github.com/envoyproxy/protoc-gen-validate@latest
  27. go install golang.org/x/tools/cmd/deadcode@latest
  28. go install mvdan.cc/gofumpt@latest
  29. go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
  30. go install github.com/ofabry/go-callvis@latest
  31. go install go.uber.org/nilaway/cmd/nilaway@latest
  32. go install github.com/fzipp/gocyclo/cmd/gocyclo@latest

安装代码规范检测工具

这个弥补 golint 的不足

brew install golangci-lint

Vim编译器配置

vim-go 必须保证 vim 版本是8以上,运行 vim 命令查看,可以通过源码安装,如果已经是是请忽略。

  1. yum remove vim
  2. github下载源码:https://github.com/vim/vim
  3. cd ./vim/src
  4. ./configure
  5. make && make install

提前下载 vim 插件源码包:

  1. mkdir ~/.vim/bundle
  2. cd ~/.vim/bundle
  3. git clone https://github.com/VundleVim/Vundle.vim.git
  4. git clone https://github.com/fatih/vim-go.git
  5. git clone https://github.com/dgryski/vim-godef
  6. git clone https://github.com/Blackrush/vim-gocode.git
  7. git clone https://github.com/mattn/vim-goimports.git
  8. git clone https://github.com/jstemmer/gotags.git
  9. mkdir ~/.vim/colors
  10. cd ~/.vim/colors
  11. git clone https://github.com/tomasr/molokai.git
  12. cp ~/.vim/colors/molokai/colors/molokai.vim ~/.vim/colors

vim .vimrc

  1. colorscheme molokai
  2. "molokai设置
  3. set t_Co=256
  4. let g:molokai_original = 1
  5. let g:rehash256 = 1
  6. "代码颜色区分
  7. syntax enable
  8. syntax on
  9. "tab宽度和缩进设置
  10. set tabstop=4
  11. set softtabstop=4
  12. set shiftwidth=4
  13. "兼容老版本
  14. set nocompatible
  15. set backspace=indent,eol,start
  16. "自动缩紧和对齐
  17. set autoindent
  18. set smartindent
  19. "开启追踪列表选择
  20. set cscopetag
  21. set hlsearch
  22. "默认显示行号
  23. set number
  24. "自动加载和保存折叠
  25. au BufWinLeave * silent mkview
  26. au BufWinEnter * silent loadview
  27. "括号和引号自动补全
  28. "inoremap ' ''<ESC>i
  29. "inoremap " ""<ESC>i
  30. "inoremap ( ()<ESC>i
  31. "inoremap [ []<ESC>i
  32. "inoremap { {}<ESC>i
  33. "GoLang代码提示
  34. imap <F2> <C-x><C-o>
  35. "开启NerdTree
  36. map <F3> :NERDTreeMirror<CR>
  37. map <F3> :NERDTreeToggle<CR>
  38. "autocmd VimEnter * NERDTree
  39. let NERDTreeWinSize=30
  40. "文件结构显示
  41. nmap <F4> :TagbarToggle<CR>
  42. "let g:tagbar_autopreview = 1 "开启自动预览(随着光标在标签上的移动,顶部会出现一个实时的预览窗口
  43. let g:tagbar_sort = 0 "关闭排序,即按标签本身在文件中的位置排序
  44. let g:tagbar_left = 1 "默认在右边,设置在左边
  45. let g:tagbar_width = 30 "宽度
  46. let g:tagbar_type_go = {
  47. \ 'ctagstype' : 'go',
  48. \ 'kinds' : [
  49. \ 'p:package',
  50. \ 'i:imports:1',
  51. \ 'c:constants',
  52. \ 'v:variables',
  53. \ 't:types',
  54. \ 'n:interfaces',
  55. \ 'w:fields',
  56. \ 'e:embedded',
  57. \ 'm:methods',
  58. \ 'r:constructor',
  59. \ 'f:functions'
  60. \ ],
  61. \ 'sro' : '.',
  62. \ 'kind2scope' : {
  63. \ 't' : 'ctype',
  64. \ 'n' : 'ntype'
  65. \ },
  66. \ 'scope2kind' : {
  67. \ 'ctype' : 't',
  68. \ 'ntype' : 'n'
  69. \ },
  70. \ 'ctagsbin' : 'gotags',
  71. \ 'ctagsargs' : '-sort -silent'
  72. \ }
  73. "go函数追踪 ctrl+] 或 gd 追踪 ctrl+o返回
  74. autocmd FileType go nnoremap <buffer> gd :call GodefUnderCursor()<cr>
  75. autocmd FileType go nnoremap <buffer> <C-]> :call GodefUnderCursor()<cr>
  76. let g:godef_split=3 "追踪打开新tab
  77. let g:godef_same_file_in_same_window=1 "函数在同一个文件中时不需要打开新窗口
  78. "保存自动goimports
  79. let g:go_fmt_command = "goimports"
  80. let g:goimports = 1
  81. filetype off
  82. set rtp+=~/.vim/bundle/Vundle.vim
  83. call vundle#begin()
  84. Plugin 'gmarik/Vundle.vim'
  85. Plugin 'fatih/vim-go'
  86. Plugin 'scrooloose/nerdtree'
  87. Plugin 'majutsushi/tagbar'
  88. call vundle#end()
  89. filetype plugin indent on

安装 vim go 工具,这里需要保证go工具命令目录在环境变量或者bin目录可以找到:

  1. vim
  2. :GoInstallBinaries

安装 vimrc 中 包含的 Plugin 插件:

  1. vim
  2. :PluginInstall

汇总一下快捷键命令:

  • F2:代码提示
  • F3:开启关闭NERDTree(也可以:NERDTree)
  • F4:右侧展示文件结构
  • Ctrl + ]/o:代码追踪/回退

更多Vim操作参考:一篇文章教你玩转vim神器写代码_程序猿的世界-CSDN博客_vim写代码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/340658
推荐阅读
相关标签
  

闽ICP备14008679号