一、環境準備:
系統環境說明:
[root@docker golang]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@docker golang]# uname -a Linux docker 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux [root@docker golang]#
準備一個go文件,用來觀察設定插件過程中的變更:
//hellogolang.go package main import "fmt" func main() { fmt.Println("Hello Golang!") }
二、外掛程式配置之路:
1、Vundle.vim:
#mkdir ~/.vim/bundle #git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
設定vimrc:建立~/ .vimrc檔(如果你沒有這個檔案的話),在檔案頂端加入有關Vundle.vim的設定:
set nocompatible " be iMproved, required filetype off " required " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " let Vundle manage Vundle, required Plugin 'gmarik/Vundle.vim' " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required
此時Vim只安裝了Vundle.vim這一個外掛。編輯hellogolang.go時與編輯普通文字檔無異,一切都還是Vim的預設屬性
#2、vim-go
Vim-go是目前使用最廣泛的用來搭建Golang開發環境的vim插件,這裡我同樣使用vim-go作為核心和基礎進行環境建構的。 vim-go利 用開源Vim插件管理器安裝,gmarik/Vundle.vim是目前被推薦次數更多的Vim插件管理器,超過了pathogen。
這裡我們 就用vundle當Vim的外掛管理工具。
編輯~/.vimrc,在vundle#begin和vundle#end間增加一行:
Plugin 'fatih/vim-go'
在Vim內執行:PluginInstall,
#Vundle.vim會在左側開啟一個Vundle Installer Preview子窗口,視窗下方會提示:“Processing 'fatih/vim-go'”,待安裝完畢後,提示訊息變成“Done!”。
這時,我們可以看到.vim/bundle下多了一個vim-go資料夾:
$ ls .vim/bundle/ vim-go/ Vundle.vim/
此時,再次編輯hellogolang.go,語法高亮有了, 保存時自動format(利用$GOBIN/gofmt)也有了,但其他高級功能,比如自動import缺少的package、自動補齊仍然沒有,我們還要繼續安裝一些東東。
3、安裝go.tools Binaries
vim-go安裝說明中提到所有必要的binary都需要先安裝好,例如gocode、godef、goimports等。
通過:GoInstallBinaries,這些vim-go依賴的二進位工具將會自動被下載,並被安裝到$GOBIN下或$GOPATH/bin下。 (這個工具需要依賴git或hg,需要提前安裝到你的OS中。)
:GoInstallBinaries的執行是互動的,你需要回車確認:
vim-go: gocode not found. Installing github.com/nsf/gocode to folder /home/tonybai/go/bin vim-go: goimports not found. Installing code.google.com/p/go.tools/cmd/goimports to folder /home/tonybai/go/bin/ vim-go: godef not found. Installing code.google.com/p/rog-go/exp/cmd/godef to folder /home/tonybai/go/bin/ vim-go: oracle not found. Installing code.google.com/p/go.tools/cmd/oracle to folder /home/tonybai/go/bin/ vim-go: gorename not found. Installing code.google.com/p/go.tools/cmd/gorename to folder /home/tonybai/go/bin/ vim-go: golint not found. Installing github.com/golang/lint/golint to folder /home/tonybai/go/bin/ vim-go: errcheck not found. Installing github.com/kisielk/errcheck to folder /home/tonybai/go/bin/
不過這些程式碼多在code.google.com上託管,因此由於眾所周知的原因,vim-go的自動安裝很可能以失敗告終,這樣就需要你根據上面日誌中提到的各個工具的源碼地址逐一去下載並本地安裝。無法搭梯子的,可以透過http://gopm.io 下載相關包。
安裝後,$GOBIN下的新增Binaries如下:
-rwxr-xr-x 1 tonybai tonybai 5735552 11?? 7 11:03 errcheck* -rwxr-xr-x 1 tonybai tonybai 9951008 11?? 7 10:33 gocode* -rwxr-xr-x 1 tonybai tonybai 5742800 11?? 7 11:07 godef* -rwxr-xr-x 1 tonybai tonybai 4994120 11?? 7 11:00 goimports* -rwxr-xr-x 1 tonybai tonybai 5750152 11?? 7 11:03 golint* -rwxr-xr-x 1 tonybai tonybai 6381832 11?? 7 11:01 gorename* -rwxr-xr-x 1 tonybai tonybai 2954392 11?? 7 10:38 gotags* -rwxr-xr-x 1 tonybai tonybai 9222856 11?? 7 11:01 oracle*
安裝好這些Binaries後,我們來看看哪些特性被支援了。
再次編輯hellogolang.go:
- 新起一行輸入fmt.,然後ctrl x, ctrl o,Vim 會彈出補齊提示下拉框,不過並非實時跟隨的那種補齊,這個補齊是由gocode提供的。
– 輸入一行程式碼:time.Sleep(time.Second),執行:GoImports,Vim會自動匯入time套件。
– 將遊標移到Sleep函數上,執行:GoDef或命令模式下敲入gd,Vim會開啟$GOROOT/src/time/sleep.go中 的Sleep函數的定義。執行:b 1返回到hellogolang.go。
– 執行:GoLint,運行golint在目前Go原始檔上。
– 執行:GoDoc,開啟目前遊標對應符號的Go文件。
– 執行:GoVet,在目前目錄下運行go vet在目前Go原始檔上。
– 執行:GoRun,編譯執行目前main package。
– 執行:GoBuild,編譯目前包,這取決於你的原始文件,GoBuild不產生結果文件。
– 執行:GoInstall,安裝目前套件。
– 執行:GoTest,測試你目前路徑下地_test.go檔。
– 執行:GoCoverage,建立一個測試覆蓋結果文件,並開啟瀏覽器展示目前套件的情況。
– 執行:GoErrCheck,檢查目前包種可能的未捕獲的errors。
– 執行:GoFiles,顯示目前套件對應的來源檔案清單。
– 執行:GoDeps,顯示目前套件的依賴套件清單。
– 執行:GoImplements,顯示目前類型實作的interface清單。
– 執行:GoRename [to],將目前遊標下的符號替換為[to]。
三、其他外掛程式
到目前為止,我們還有若干特性沒能實現,重點是:
– 即時跟隨的程式碼補齊
– Code Snippet support
1、安裝YCM(Your Complete Me)
在~/.vimrc中加入一行:
#Plugin 'Valloric /YouCompleteMe'
儲存退出後,再開啟~/.vimrc並執行:PluginInstall
sudo yum install build-essential cmake python-dev cd ~/.vim/bundle/YouCompleteMe ./install.sh建置(編譯C 很慢,需要耐心的等一會兒)ok後,再開啟hellogolang.go,逐字的即時補全功能就具備了! Cool!
Vim-go默认是用ultisnips引擎插件,但这个插件需要单独安装。同样,我们利用vundle来安装它,在~/.vimrc中添加一行:
Plugin 'SirVer/ultisnips'
保存,退出vim
打开vim,执行:PluginInstall
编辑hellogolang.go,按照go.snip中的说明,我们输入func后敲击tab键,我们发现期待的:
func name(params) type { }
并没有出现。反倒是YCM的下拉提示显示在那里让你选择。似乎是ultisnips和YCM的键组合冲突了。ultisnips官方说明也的确如 此。ultisnips默认是用Tab展开snippet的,而YCM中的Tab用来选择补齐项,我们可以通过设置来避免这些。
我们在.vimrc中添加如下setting:
" YCM settings let g:ycm_key_list_select_completion = ['', ''] let g:ycm_key_list_previous_completion = [''] let g:ycm_key_invoke_completion = '<C-Space>' " UltiSnips setting let g:UltiSnipsExpandTrigger="<tab>" let g:UltiSnipsJumpForwardTrigger="<c-b>" let g:UltiSnipsJumpBackwardTrigger="<c-z>"
这样让YCM通过回车和向下的箭头来做list item正向选择,通过向上箭头做反向选择。通过ctrl+space来原地触发补齐提示。
而ultisnips则是用tab做snippet展开,ctrl+b正向切换占位符,ctrl+z反向切换占位符。
四、.vimrc
前面讲到了vim-go有许多命令,在:xx模式下执行多显不便,于是你可以定义一些Mappings,比如:
" set mapleader let mapleader = "," " vim-go custom mappings au FileType go nmap <Leader>s <Plug>(go-implements) au FileType go nmap <Leader>i <Plug>(go-info) au FileType go nmap <Leader>gd <Plug>(go-doc) au FileType go nmap <Leader>gv <Plug>(go-doc-vertical) au FileType go nmap <leader>r <Plug>(go-run) au FileType go nmap <leader>b <Plug>(go-build) au FileType go nmap <leader>t <Plug>(go-test) au FileType go nmap <leader>c <Plug>(go-coverage) au FileType go nmap <Leader>ds <Plug>(go-def-split) au FileType go nmap <Leader>dv <Plug>(go-def-vertical) au FileType go nmap <Leader>dt <Plug>(go-def-tab) au FileType go nmap <Leader>e <Plug>(go-rename)
这样我们在命令模式下,输入+
另外下面这个配置使得我们在save file时既可以格式化代码,又可以自动插入包导入语句(或删除不用的包导入语句)。
" vim-go settings let g:go_fmt_command = "goimports"
到这里,我们的Vim Golang开发环境就基本搭建好了。snippet+实时补齐让你Coding如飞!
五、.vimrc文件
下面是截至目前为止全量.vimrc文件的内容: set nocompatible " be iMproved, required filetype off " required colorscheme molokai " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " let Vundle manage Vundle, required Plugin 'gmarik/Vundle.vim' Plugin 'fatih/vim-go' Plugin 'Valloric/YouCompleteMe' Plugin 'SirVer/ultisnips' " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required " set mapleader let mapleader = "," " vim-go custom mappings au FileType go nmaps (go-implements) au FileType go nmap i (go-info) au FileType go nmap gd (go-doc) au FileType go nmap gv (go-doc-vertical) au FileType go nmap r (go-run) au FileType go nmap b (go-build) au FileType go nmap t (go-test) au FileType go nmap c (go-coverage) au FileType go nmap ds (go-def-split) au FileType go nmap dv (go-def-vertical) au FileType go nmap dt (go-def-tab) au FileType go nmap e (go-rename) " vim-go settings let g:go_fmt_command = "goimports" " YCM settings let g:ycm_key_list_select_completion = ['', ''] let g:ycm_key_list_previous_completion = ['', ''] let g:ycm_key_invoke_completion = ' ' " UltiSnips settings let g:UltiSnipsExpandTrigger=" " let g:UltiSnipsJumpForwardTrigger=" " let g:UltiSnipsJumpBackwardTrigger=" "
更多golang知识请关注golang教程栏目。
以上是go語言環境vim配置詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Toensureinitfunctionsareeffectiveandmaintainable:1)Minimizesideeffectsbyreturningvaluesinsteadofmodifyingglobalstate,2)Ensureidempotencytohandlemultiplecallssafely,and3)Breakdowncomplexinitializationintosmaller,focusedfunctionstoenhancemodularityandm

goisidealforbeginnersandsubableforforcloudnetworkservicesduetoitssimplicity,效率和concurrencyFeatures.1)installgromtheofficialwebsitealwebsiteandverifywith'.2)

開發者應遵循以下最佳實踐:1.謹慎管理goroutines以防止資源洩漏;2.使用通道進行同步,但避免過度使用;3.在並發程序中顯式處理錯誤;4.了解GOMAXPROCS以優化性能。這些實踐對於高效和穩健的軟件開發至關重要,因為它們確保了資源的有效管理、同步的正確實現、錯誤的適當處理以及性能的優化,從而提升軟件的效率和可維護性。

Goexcelsinproductionduetoitsperformanceandsimplicity,butrequirescarefulmanagementofscalability,errorhandling,andresources.1)DockerusesGoforefficientcontainermanagementthroughgoroutines.2)UberscalesmicroserviceswithGo,facingchallengesinservicemanageme

我們需要自定義錯誤類型,因為標準錯誤接口提供的信息有限,自定義類型能添加更多上下文和結構化信息。 1)自定義錯誤類型能包含錯誤代碼、位置、上下文數據等,2)提高調試效率和用戶體驗,3)但需注意其複雜性和維護成本。

goisidealforbuildingscalablesystemsduetoitssimplicity,效率和建築物內currencysupport.1)go'scleansyntaxandaxandaxandaxandMinimalisticDesignenhanceProductivityAndRedCoductivityAndRedCuceErr.2)ItSgoroutinesAndInesAndInesAndInesAndineSandChannelsEnablenableNablenableNableNablenableFifficConcurrentscorncurrentprogragrammentworking torkermenticmminging

Initfunctionsingorunautomationbeforemain()andareusefulforsettingupenvorments和InitializingVariables.usethemforsimpletasks,避免使用輔助效果,andbecautiouswithTestingTestingTestingAndLoggingTomaintAnainCodeCodeCodeClarityAndTestesto。

goinitializespackagesintheordertheordertheyimported,thenexecutesInitFunctionswithinApcageIntheirdeFinityOrder,andfilenamesdetermineTheOrderAcractacractacrosmultiplefiles.thisprocessCanbeCanbeinepessCanbeInfleccessByendercrededBydeccredByDependenciesbetenciesbetencemendencenciesbetnependendpackages,whermayleLeadtocomplexinitialitialializizesizization


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。