源码安装GoLang

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

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

export GOROOT=/usr/local/go
export GOPATH=/Users/why/Desktop/go
export PATH=$PATH/bin:$GOROOT/bin:$GOPATH/bin

alias gosrc="cd $GOROOT/src"
alias gopath="cd $GOPATH"
alias gomod="cd $GOMOD"

alias gorm='gormt -g=true'
alias gotest='gotests -all -w ./'
alias goformat='gofmt -l -w -s ./'
alias goimport='goimports -l -w ./'
alias gorace='go run -race .'
alias cover="go test -coverprofile=cover.out"
alias covers="go tool cover -html=cover.out"

配置go env:

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

安装Go相关工具

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

go install mvdan.cc/gofumpt@latest
go install github.com/ofabry/go-callvis@latest
go install github.com/google/gops@latest
go install github.com/mdempsky/gocode@latest
go install golang.org/x/tools/gopls@latest
go install github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest
go install github.com/ramya-rao-a/go-outline@latest
go install github.com/acroca/go-symbols@latest
go install golang.org/x/tools/cmd/guru@latest
go install golang.org/x/tools/cmd/gorename@latest
go install github.com/go-delve/delve/cmd/dlv@latest
go install github.com/rogpeppe/godef@latest
go install github.com/sqs/goreturns@latest
go install golang.org/x/lint/golint@latest
go get -u -v github.com/cweill/gotests/...
go install github.com/fatih/gomodifytags@latest
go install github.com/josharian/impl@latest
go install github.com/davidrjenni/reftools/cmd/fillstruct@latest
go install github.com/haya14busa/goplay/cmd/goplay@latest
go install github.com/godoctor/godoctor@latest
go install github.com/smartystreets/goconvey@latest  
go install github.com/jstemmer/gotags@latest
go install golang.org/x/tools/cmd/goimports@latest 
go install golang.org/x/tools/cmd/godoc@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/xxjwxc/gormt@latest(gormt -g=true)
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest 
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/envoyproxy/protoc-gen-validate@latest

安装代码规范检测工具

这个弥补 golint 的不足

brew install golangci-lint

Vim编译器配置

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

yum remove vim
github下载源码:https://github.com/vim/vim
cd ./vim/src
./configure
make && make install

提前下载 vim 插件源码包:

mkdir ~/.vim/bundle  
cd ~/.vim/bundle 

git clone https://github.com/VundleVim/Vundle.vim.git
git clone https://github.com/fatih/vim-go.git
git clone https://github.com/dgryski/vim-godef
git clone https://github.com/Blackrush/vim-gocode.git
git clone https://github.com/mattn/vim-goimports.git
git clone https://github.com/jstemmer/gotags.git

mkdir ~/.vim/colors 
cd ~/.vim/colors
git clone https://github.com/tomasr/molokai.git
cp ~/.vim/colors/molokai/colors/molokai.vim  ~/.vim/colors 

vim .vimrc

colorscheme molokai
 
"molokai设置
set t_Co=256
let g:molokai_original = 1 
let g:rehash256 = 1 

"代码颜色区分
syntax enable
syntax on
 
"tab宽度和缩进设置
set tabstop=4
set softtabstop=4
set shiftwidth=4

"兼容老版本
set nocompatible
set backspace=indent,eol,start

"自动缩紧和对齐
set autoindent
set smartindent
 
"开启追踪列表选择
set cscopetag
set hlsearch

"默认显示行号
set number
 
"自动加载和保存折叠
au BufWinLeave * silent mkview
au BufWinEnter * silent loadview

"括号和引号自动补全
"inoremap ' ''<ESC>i
"inoremap " ""<ESC>i
"inoremap ( ()<ESC>i
"inoremap [ []<ESC>i
"inoremap { {}<ESC>i

"GoLang代码提示
imap <F2> <C-x><C-o>

"开启NerdTree
map <F3> :NERDTreeMirror<CR>
map <F3> :NERDTreeToggle<CR>
"autocmd VimEnter * NERDTree
let NERDTreeWinSize=30

"文件结构显示
nmap <F4> :TagbarToggle<CR>
"let g:tagbar_autopreview = 1 "开启自动预览(随着光标在标签上的移动,顶部会出现一个实时的预览窗口 
let g:tagbar_sort = 0 "关闭排序,即按标签本身在文件中的位置排序
let g:tagbar_left = 1 "默认在右边,设置在左边
let g:tagbar_width = 30 "宽度
let g:tagbar_type_go = { 
    \ 'ctagstype' : 'go',
    \ 'kinds'     : [ 
        \ 'p:package',
        \ 'i:imports:1',
        \ 'c:constants',
        \ 'v:variables',
        \ 't:types',
        \ 'n:interfaces',
        \ 'w:fields',
        \ 'e:embedded',
        \ 'm:methods',
        \ 'r:constructor',
        \ 'f:functions'
    \ ],
    \ 'sro' : '.',
    \ 'kind2scope' : { 
        \ 't' : 'ctype',
        \ 'n' : 'ntype'
    \ },
    \ 'scope2kind' : { 
        \ 'ctype' : 't',
        \ 'ntype' : 'n'
    \ },
    \ 'ctagsbin'  : 'gotags',
    \ 'ctagsargs' : '-sort -silent'
\ }

"go函数追踪 ctrl+] 或 gd 追踪 ctrl+o返回
autocmd FileType go nnoremap <buffer> gd :call GodefUnderCursor()<cr>
autocmd FileType go nnoremap <buffer> <C-]> :call GodefUnderCursor()<cr>
let g:godef_split=3    "追踪打开新tab
let g:godef_same_file_in_same_window=1    "函数在同一个文件中时不需要打开新窗口

"保存自动goimports
let g:go_fmt_command = "goimports"
let g:goimports = 1

filetype off

set rtp+=~/.vim/bundle/Vundle.vim

call vundle#begin()

Plugin 'gmarik/Vundle.vim'
Plugin 'fatih/vim-go'
Plugin 'scrooloose/nerdtree'
Plugin 'majutsushi/tagbar'

call vundle#end()

filetype plugin indent on

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

vim
:GoInstallBinaries

安装 vimrc 中 包含的 Plugin 插件:

vim
:PluginInstall

汇总一下快捷键命令:

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