Skip to main content

搭建Go开发环境

Go 环境安装#

下载地址:https://go.dev/dl/

下载具体安装包, 以下载Linux安装包为例

$ wget https://golang.google.cn/dl/go1.17.10.linux-amd64.tar.gz

解压并安装

$ tar -xvzf go1.17.10.linux-amd64.tar.gz -C /usr/local
# 查看安装目录$ ll /usr/local/go
# 查看安装的版本$ go version# Outputgo version go1.17.10 linux/amd64

环境变量配置#

vim $HOME/.bashrc

/.bashrc可以替换为 .zshrc, 配置文件以自己使用的为准


tee -a $HOME/.bashrc <<'EOF'# go envexport GOROOT="/usr/local/go"             # GOROOT 设置export GOPATH=$HOME/go                    # GOPATH 设置export PATH=$GOROOT/bin:$GOPATH/bin:$PATH # 加入到 PATH 路径export GO111MODULE="on"                   # 开启 Go moudlesexport GOPROXY=https://goproxy.cn,direct  # 代理服务器设置export GOSUMDB=off                        # 关闭校验 Go 依赖包的哈希值EOF

ProtoBuf 环境安装#

安装 protoc 编译器#

$ PB_REL="https://github.com/protocolbuffers/protobuf/releases"# 如果是macOS 可以改为 osx$ OS="linux"$ VERSION="3.19.4"$ curl -LO $PB_REL/download/v$VERSION/protoc-$VERSION-$OS-x86_64.zip
$ unzip protoc-$VERSION-$OS-x86_64.zip -d /usr/local
$ export PATH="$PATH:/usr/local/bin"

查看版本

protoc --versionlibprotoc 3.15.6

安装 protoc-gen-go 插件#

运行:

go get -u github.com/golang/protobuf/{helloworld,protoc-gen-go}

编译后会安装 protoc-gen-go$GOBIN 目录, 默认在 $GOPATH/bin.
该目录必须在系统的环境变量 $PATH中,这样在编译 .proto 文件时 protocol 编译器才能找到插件。

编译安装 protoc#

如果需要,也可以进行编译安装

# 安装依赖工具$ sudo yum -y install make autoconf automake cmake perl-CPAN libcurl-devel libtool gcc gcc-c++ glibc-headers zlib-devel git-lfs telnet ctags lrzsz jq expat-devel openssl-devel
# 编译安装 protoc
# 第一步:安装 protobuf$ cd /tmp/$ git clone --depth=1 https://github.com/protocolbuffers/protobuf$ cd protobuf$ ./autogen.sh$ ./configure$ make$ sudo make install$ protoc --version # 查看 protoc 版本,成功输出版本号,说明安装成功libprotoc 3.15.6
# 第二步:安装 protoc-gen-go$ go get -u github.com/golang/protobuf/protoc-gen-go

Go 开发 IDE 安装和配置#

主流的有三种

  • Goland
  • VSCode
  • Vim(NeoVim)

可以根据个人爱好来进行选择。

安装vim#

这里以 NeoVim 为例进行安装

# Linux$ sudo pip3 install pynvim$ sudo yum -y install neovim
# macOSbrew install neovimpython3 -m pip install pynvim

配置bashrc#

tee -a $HOME/.bashrc <<'EOF'# Configure for nvimexport EDITOR=nvim # 默认的编辑器alias vim="nvim"EOF

检查 nvim 是否安装成功#

$ bash$ vi --version # 输出 NVIM v0.3.8 说明安装成功NVIM v0.3.8Build type: RelWithDebInfo...

配置 neovim#

安装 plug 插件#

# see: see: https://github.com/junegunn/vim-plug#toc1sh -c 'curl -fLo ~/.config/nvim/autoload/plug.vim --create-dirs \       https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'

配置 init.vim#

# vim ~/.config/nvim/init.vimlet mapleader = ","# find python3: which python3let g:python3_host_prog = '/usr/local/bin/python3'
filetype onfiletype indent onfiletype plugin on
set encoding=UTF-8
syntax onset nocompatibleset hlsearchset number relativenumberset laststatus=2set vbset rulerset spelllang=en_usset autoindentset colorcolumn=80set mouse=aset clipboard=unnamedset noscrollbindset wildmenuset autochdir
hi Search cterm=NONE ctermfg=black ctermbg=red
" No more Arrow Keys, deal with itnoremap <Up> <NOP>noremap <Down> <NOP>noremap <Left> <NOP>noremap <Right> <NOP>
" netrw
nnoremap - :Explore<CR>let g:netrw_banner = 0let g:netrw_liststyle = 3let g:netrw_bufsettings = 'noma nomod nu nobl nowrap ro'autocmd FileType netrw setl bufhidden=delete
"-- netrw END
" plug call plug#begin()"> Must HavePlug 'vim-airline/vim-airline' " https://github.com/vim-airline/vim-airlinePlug 'ctrlpvim/ctrlp.vim'      " https://github.com/ctrlpvim/ctrlp.vimPlug 'ryanoasis/vim-devicons'  " https://github.com/ryanoasis/vim-devicons + https://github.com/ryanoasis/nerd-fonts/Plug 'tpope/vim-commentary'    " https://github.com/tpope/vim-commentaryPlug 'airblade/vim-gitgutter'  " https://github.com/airblade/vim-gitgutterPlug 'mkitt/tabline.vim'       " https://github.com/mkitt/tabline.vimPlug 'github/copilot.vim'      " https://github.com/github/copilot.vim
"> GoPlug 'fatih/vim-go', { 'do': ':GoInstallBinaries' } " https://github.com/fatih/vim-goPlug 'neoclide/coc.nvim', {'branch': 'release'}     " https://github.com/neoclide/coc.nvimPlug 'SirVer/ultisnips'                             " https://github.com/sirver/UltiSnips
"> ThemePlug 'NLKNguyen/papercolor-theme' " https://github.com/NLKNguyen/papercolor-themecall plug#end()
"-- plug END
" ctrlpset runtimepath^=~/.vim/bundle/ctrlp.vimlet g:ctrlp_user_command = ['.git', 'cd %s && git ls-files -co --exclude-standard']
" vim-gitgutter
set updatetime=500
"-- vim-gitgutter END
" papercolor-theme
set termguicolorsset background=darkcolorscheme PaperColor
"-- papercolor-theme END

安装插件#

使用 vim 打开任一文件,执行以下命令来安装依赖包:

:PlugInstall

插件默认安装在 ~/.config/nvim/plugged 目录下

执行健康检查#

:checkhealth

配置插件 go.vim#

创建目录 mkdir -p ~/.config/nvim/ftdetect

新建文件并加入以下内容: ~/.config/nvim/ftdetect/go.vim

# vim ~/.config/nvim/ftdetect/go.vim"-- vim-go specific configuration
" run :GoBuild or :GoTestCompile based on the go filefunction! s:build_go_files()  let l:file = expand('%')  if l:file =~# '^\f\+_test\.go$'    call go#test#Test(0, 1)  elseif l:file =~# '^\f\+\.go$'    call go#cmd#Build(0)  endifendfunction
autocmd FileType go nmap <leader>b :<C-u>call <SID>build_go_files()<CR>autocmd FileType go nmap <Leader>c <Plug>(go-coverage-toggle)autocmd FileType go nmap <leader>t <Plug>(go-test)
autocmd Filetype go command! -bang A call go#alternate#Switch(<bang>0, 'edit')autocmd Filetype go command! -bang AV call go#alternate#Switch(<bang>0, 'vsplit')autocmd Filetype go command! -bang AS call go#alternate#Switch(<bang>0, 'split')autocmd Filetype go command! -bang AT call go#alternate#Switch(<bang>0, 'tabe')
autocmd FileType go setlocal foldmethod=expr foldexpr=getline(v:lnum)=~'^\s*'.&commentstring[0]
let g:go_list_type = "quickfix"    " error lists are of type quickfixlet g:go_fmt_command = "goimports" " automatically format and rewrite importslet g:go_auto_sameids = 1          " highlight matching identifiers
"-- highlight configlet g:go_highlight_types = 1let g:go_highlight_fields = 1let g:go_highlight_functions = 1let g:go_highlight_function_calls = 1let g:go_highlight_extra_types = 1let g:go_highlight_generate_tags = 1
"-- vim-go specific configuration (END)
"-- coc.nvim specific configuration"-- see: https://github.com/neoclide/coc.nvim#example-vim-configuration"-- see: https://unpkg.com/coc.nvim@0.0.77/doc/coc.cnx
set hiddenset cmdheight=2set updatetime=300set shortmess+=cif has("patch-8.1.1564")  set signcolumn=numberelse  set signcolumn=yesendif
nmap <silent> gd <Plug>(coc-definition)nmap <silent> gy <Plug>(coc-type-definition)nmap <silent> gr <Plug>(coc-references)nmap <silent> gi <Plug>(coc-implementation)nmap <silent> rn <Plug>(coc-rename)
nnoremap <silent> K :call <SID>show_documentation()<CR>function! s:show_documentation()  if (index(['vim','help'], &filetype) >= 0)    execute 'h '.expand('<cword>')  else    call CocAction('doHover')  endifendfunction
"-- coc.nvim specific configuration (END)

配置go补全#

新建文件,如果没有

vim ~/.config/nvim/coc-settings.json

加入以下内容

{    "languageserver": {        "golang": {            "command": "gopls",            "rootPatterns": [                "go.mod"            ],            "filetypes": [                "go"            ]        }    },    "suggest.noselect": false,    "coc.preferences.diagnostic.displayByAle": true,    "suggest.floatEnable": true}

安装扩展

注意: 需要安装 node,否则安装无效

:CocInstall coc-json coc-tsserver
:CocInstall coc-python

Reference#