GO語言學習

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

ubuntu 14.04 GO 語言編譯環境的安裝

1. 利用ubuntu的庫安裝

sudo apt-get install golang

但是用ubuntu的庫安裝有幾個不好的地方:

  • golang的版本由ubuntu的庫決定,ubuntu14.04目前提供的golang 1.2.1
  • golang的語言的環境設定已經預設設定,比如GOROOT設定為/usr/local/bin,GOPATH等

因此建議不要使用ubuntu的庫安裝golang環境

2. 使用golang官方文檔安裝

golang社區的安裝指導https://golang.org/doc/install

2.1 卸載ubuntu14.04庫安裝的go

  • 卸載golang-go

    sudo apt-get  remove golang-go
  • 卸載golang-go及其依賴

    sudo apt-get remove --auto-remove golang-go
  • 卸載golang-go並刪除其本地和設定檔

    sudo apt-get purge golang-go
  • 卸載golang-go及其依賴並刪除其本地和設定檔

    sudo apt-get purge --auto-remove golang-go

2.2利用編譯好的檔案安裝

wget https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gztar -C /usr/local -zxf go1.6.2.linux-amd64.tar.gz

設定環境變數,修改etc/profile檔案,使預設搜尋go所在位置

export PATH=$PATH:/usr/local/go/bin

如果go安裝在其他位置,將其他位置加入PATH路徑中

2.3 測試GO安裝

設定go的工作環境,修改$HOME/.profile檔案

export GOPATH=$HOME/gowork

測試hello-world

mkdir $HOME/gowork/src/hellocd $HOME/gowork/src/hello && touch hello.go

將以下代碼輸入hello.go

package mainimport "fmt"func main() {    fmt.Printf("hello, world\n")}

編譯和測試

go install hello$GOPATH/bin/hello

3. vim go 語言編寫環境配置

3.1 vim go語言文法高亮

  1. 由於neocomplete外掛程式要求vim7.4及以上,並且要求vim 支援lua,但是ubuntu 14.04 庫安裝的vim不支援lua,所以需要卸載後從原始碼安裝。 參考http://winter233.com/Using-Vim-with-Neocomplete/

    // 卸載原來的vim, 如果原來沒有gvim,則命令中刪除gvimsudo apt-get remove vim vim-runtime gvim//安裝vim依賴的包和工具,如果不需要案頭環境的支援(gvim),則在命令中刪除gnome\gnomeui\libgtk\libatk\libbonoboui\libcairo\libx11\libxpm\libxt, 如果使用git而不是hg,則刪除mercurialsudo apt-get install libncurses5-dev libgnome2-dev libgnomeui-dev \libgtk2.0-dev libatk1.0-dev libbonoboui2-dev \libcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev \ruby-dev mercurial// install luasudo apt-get install liblua5.2-dev lua5.2//compile vim74 from source, 如果沒有案頭環境,則可刪除<--enable-gui=gtk2>wget ftp://ftp.vim.org/pub/vim/unix/vim-7.4.tar.bz2tar xvf vim-7.4.tar.bz2cd vim74./configure --with-features=huge \        --enable-multibyte \        --enable-rubyinterp \        --enable-pythoninterp \        --with-python-config-dir=/usr/lib/python2.7/config \        --enable-perlinterp \        --enable-luainterp \        --enable-gui=gtk2 --enable-cscope --prefix=/usr//set vim74 rumtimedir, and finish installmake VIMRUNTIMEDIR=/usr/share/vim/vim74sudo make install//Check installation by typing vim --version in shell or :echo has("lua") in vim.
  2. 安裝vundle,用來管理vim外掛程式, vundle在github上,https://github.com/VundleVim/Vundle.vim

    git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

    配置$HOME目錄下的.vimrc檔案,從連結地址上粘貼過來即可(略)

  3. 安裝vim-go, vim的go語言外掛程式,https://github.com/fatih/vim-go 修改.vimrc檔案,添加如下:

    Plugin 'fatih/vim-go'
  4. vim-go要完全工作,還需要一些必要的工具(比如gocode, godef, goimport etc),這些工具可以通過vim內建的:GoInstallBinaries命令安裝,如果之前安裝過,可以通過:GoUpdateBinaries 更新,這樣安裝會使gocode等工具安裝在$GOPATH/bin目錄下。

  5. 安裝neocomplete.vim, https://github.com/Shougo/neocomplete.vim這是vim-go推薦的即時自動補全的外掛程式

    Plugin 'Shougo/neocomplete.vim'
  6. 安裝ctags + gotags + tagbar, 其中由於ctags不支援go,所以使用與ctags相容的gotags。

    // install ctagswget http://prdownloads.sourceforge.net/ctags/ctags-5.8.tar.gztar -zxf ctags-5.8.tar.gzcd ctags && ./configure && make && make install// install gotags, 具體見gotags的githubgo get -u github.com/jstemmer/gotags// add config to .vimrc for tagbarlet 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'\ }//install tag using vundle Plugin 'majutsushi/tagbar'//add config to .vimrc for quick start, 具體見tagbar的githubnmap <F8> :TagbarToggle<CR>
  7. vim配置完之後,需要重新登陸終端才會生效。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.