除了Java、Python和JavaScript之外,如果要開始學習一門新語言的話,我想應該是Go!

創新互聯建站堅持“要么做到,要么別承諾”的工作理念,服務領域包括:做網站、網站制作、企業官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯網時代的雙遼網站設計、移動媒體設計的需求,幫助企業找到有效的互聯網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!
Go語言正在被越來越多的公司使用。我們公司的后端服務已經全面采用Go語言實現了。
最開始接觸Go語言是去年將一份Go代碼“翻譯”成Python并集成到測試平臺上,說來也挺神奇,我從來沒學過Go卻完成了這個工作,這也側面反應了Go的語法還是很平易近人的。
今年,在海翔飛調崗之后已經沒有太多時間寫代碼了,但如果要開始學習一個新的語言或技術的話,我最想學的是Go!
目前來看,Go似乎還并沒有太多測試人員使用的場景,不過,我之前介紹過的BDD行為驅動框架gauge是由Go開發的,當然,它也支持使用Go來編寫BDD測試代碼。
對于,已經有一定開發經驗的同學,如何快速的開始學習Go語言呢?我這里給一些思路。
#### 第一步:下載和安裝
在配置環境的時候你需要重點了解GOROOT、GOPATH的作用。
你還要準備一款稱手的編輯器,如果你像我一樣,一直都在使用VS Code的話,那么就它就可以了。
#### 第二步:從hello world開始
先運行一個hello world程序,認識Go語言的語法。
package main
import ( ? ?"fmt")
func main(){
fmt.Println("helloworld!")
}
#### 第三步:熟悉Go的語法
接下來,你可能要花一周左右的時間熟悉Go語言的語法。比如,變量定義、if/for、函數、Map、跨文件的程序調用…等,當然,還有一些Go特有的知識。
當然,我更喜歡看視頻教程,雖然質量參差不齊,但我仍然覺得看視頻比我自己看書更有效率。
熟悉一段Go代碼:
package main
import"fmt"
func myFunc() {
i := 0
Here: ? //這行的第一個詞,以冒號結束作為標簽
fmt.Println(i)
i++ ? ?if i 10{ ? ? ? ?goto Here ? //跳轉到Here去
}
}func main() { ? ?//調用函數
myFunc()
}
#### 第四步:Go如何做單元測試
針對Go做測試也非常簡單。比如,這是一個被測試文件:add.go。
package test_demofunc Add(a int, b int) int{ ? ?return a + b
}
下面針對Add()函數編寫測試用例,test_add.go
package test_demo
import ( ? ?"testing")
func TestAdd1(t *testing.T){
r:= Add(1, 2) ? ?if r !=3{
t.Errorf("Add(1, 2)failed. Got %d, expected 3.", r)
}
}
func TestAdd2(t *testing.T){
r:= Add(2, 2) ? ?if r !=4{
t.Errorf("Add(2, 2)failed. Got %d, expected 4.", r)
}
}
你只需要執行?go test?命令就可以運行上面的測試了。
#### 第五步:從哪兒找第三方庫
當然,你只學習go語言本身,基本是做不了什么事的,必須要使用第三方擴展庫。
這里羅列了Go語言的第三方庫,通過這些第三方庫的介紹,我們也可以大概知道Go可以用來干什么。
如果你知道庫的名字的話,也可以在這個網站上搜索。
據我了解,Go的第三方庫大多都在GitHub上面。
#### 第六步:用Go做Web開發
Go是靜態語言,而且支持并發編程,所以,他有天然的性能優勢,大多公司主要使用Go也是開發后端服務(即API)。
終于到了實戰階段,如果我們真的要掌握一門語言,那么一定要用它來開發一個項目出來。這個過程大概需要一個月。
Beego是Go下在主流的Web開發框架,資料相對比較豐富,而且有完善的文檔。你可為此制定一個目標,比如用它來開發一個Blog,為此,你需要詳細閱讀Beego文檔,以及學習相關的Web開發技術。
等你完成這個項目的時候,我想你已經會使用Go語言了。
LiteIDE是一款專門為Go語言開發的跨平臺輕量級集成開發環境(IDE),由QT編寫。
LiteIDE主要特點: 支持主流操作系統
Windows
Linux
MacOS X Go編譯環境管理和切換
管理和切換多個Go編譯環境
支持Go語言交叉編譯 與Go標準一致的項目管理方式
基于GOPATH的包瀏覽器
基于GOPATH的編譯系統
基于GOPATH的Api文檔檢索 Go語言的編輯支持
類瀏覽器和大綱顯示
Gocode(代碼自動完成工具)的完美支持
Go語言文檔查看和Api快速檢索
代碼表達式信息顯示F1
源代碼定義跳轉支持F2
Gdb斷點和調試支持
gofmt自動格式化支持 其他特征
支持多國語言界面顯示
完全插件體系結構
支持編輯器配色方案
基于Kate的語法顯示支持
基于全文的單詞自動完成
支持鍵盤快捷鍵綁定方案
Markdown文檔編輯支持
實時預覽和同步顯示
自定義CSS顯示
可導出HTML和PDF文檔
批量轉換/合并為HTML/PDF文檔 Sublime Text 2(以下簡稱Sublime)+ GoSublime + gocode + MarGo的組合。
其優點有: 自動化提示代碼。 保存的時候自動格式化代碼,讓您編寫的代碼更加美觀,符合Go的標準。 支持項目管理 支持語法高亮 熟悉Java的讀者應該對于idea不陌生,idea是通過一個插件來支持go語言的高亮語法,代碼提示和重構實現。
Go語言作為出現比較晚的一門編程語言,在其原生支持高并發、云原生等領域的優秀表現,像目前比較流行的容器編排技術Kubernetes、容器技術Docker都是用Go語言寫的,像Java等其他面向對象的語言,雖然也能做云原生相關的開發,但是支持的程度遠沒有Go語言高,憑借其語言特性和簡單的編程方式,彌補了其他編程語言一定程度上的不足,一度成為一個熱門的編程語言。
最近在學習Go語言,我之前使用過C#、Java等面向對象編程的語言,發現其中有很多的編程方式和其他語言有區別的地方,好記性不如爛筆頭,總結一下,和其他語言做個對比。這里只總結差異的地方,具體的語法不做詳細的介紹。
種一棵樹最好的時間是十年前,其次是現在。
3)變量初始化時候可以和其他語言一樣直接在變量后面加等號,等號后面為要初始化的值,也可以使用變量名:=變量值的簡單方式
3)變量賦值 Go語言的變量賦值和多數語言一致,但是Go語言提供了多重賦值的功能,比如下面這個交換i、j變量的語句:
在不支持多重賦值的語言中,交換兩個變量的值需要引入一個中間變量:
4)匿名變量
在使用其他語言時,有時候要獲取一個值,卻因為該函數返回多個值而不得不定義很多沒有的變量,Go語言可以借助多重返回值和匿名變量來避免這種寫法,使代碼看起來更優雅。
假如GetName()函數返回3個值,分別是firstName,lastName和nickName
若指向獲得nickName,則函數調用可以這樣寫
這種寫法可以讓代碼更清晰,從而大幅降低溝通的復雜度和維護的難度。
1)基本常量
常量使用關鍵字const 定義,可以限定常量類型,但不是必須的,如果沒有定義常量的類型,是無類型常量
2)預定義常量
Go語言預定義了這些常量 true、false和iota
iota比較特殊,可以被任務是一個可被編譯器修改的常量,在每個const關鍵字出現時被重置為0,然后在下一個const出現之前每出現一個iota,其所代表的數字會自動加1.
3)枚舉
1)int 和int32在Go語言中被認為是兩種不同類型的類型
2)Go語言定義了兩個浮點型float32和float64,其中前者等價于C語言的float類型,后者等價于C語言的double類型
3)go語言支持復數類型
復數實際上是由兩個實數(在計算機中使用浮點數表示)構成,一個表示實部(real)、一個表示虛部(imag)。也就是數學上的那個復數
復數的表示
實部與虛部
對于一個復數z=complex(x,y),就可以通過Go語言內置函數real(z)獲得該復數的實部,也就是x,通過imag(z)獲得該復數的虛部,也就是y
4)數組(值類型,長度在定義后無法再次修改,每次傳遞都將產生一個副本。)
5)數組切片(slice)
數組切片(slice)彌補了數組的不足,其數據結構可以抽象為以下三個變量:
6)Map 在go語言中Map不需要引入任何庫,使用很方便
Go循環語句只支持for關鍵字,不支持while和do-while
goto語句的語義非常簡單,就是跳轉到本函數內的某個標簽
今天就介紹到這里,以后我會在總結Go語言在其他方面比如并發編程、面向對象、網絡編程等方面的不同及使用方法。希望對大家有所幫助。
1、編譯vimgdb
下載vimgdb73和vim73
mkdir -p ./tmp
cd tmp
tar zxvf ../vim-7.3.tar.gz
unzip ../vimgdb-for-vim7.3-master.zip
mv vimgdb-for-vim7.3-master vimgdb-for-vim7.3
patch -p0 vimgdb-for-vim7.3/vim73.patch
cd vim73
安裝依賴
sudo apt-get install build-essential
sudo apt-get build-dep vim-gtk
sudo apt-get install libncurses5-dev
安裝
// 這里直接執行make的操作
make
sudo make install
安裝vimgdb runtime
cd ../vimgdb-for-vim7.3
cp vimgdb_runtime ~/.vim/bundle
打開vim
:helptags ~/.vim/bundle/vimgdb_runtime/doc " 生成doc文件
添加配置.vimrc
" vimgdb插件
run macros/gdb_mappings.vim
在vim中執行gdb時,報 “Unable to read from GDB pseudo tty” 的錯誤,因為沒有安裝 gdb ,所以安裝gdb
sudo apt-get install gdb
2、安裝vundle
set up vundle
$ git clone ~/.vim/bundle/vundle
Configure Plugins
在.vimrc文件的開頭添加下面的內容,有些不是必須的,可以注掉
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" alternatively, pass a path where Vundle should install plugins
"let path = '~/some/path/here'
"call vundle#rc(path)
" let Vundle manage Vundle, required
Plugin 'gmarik/vundle'
" The following are examples of different formats supported.
" Keep Plugin commands between here and filetype plugin indent on.
" scripts on GitHub repos
Plugin 'tpope/vim-fugitive'
Plugin 'Lokaltog/vim-easymotion'
Plugin 'tpope/vim-rails.git'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" scripts from
Plugin 'L9'
Plugin 'FuzzyFinder'
" scripts not on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin ''
" ...
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" : PluginList - list configured plugins
" : PluginInstall(!) - install (update) plugins
" : PluginSearch(!) foo - search (or refresh cache first) for foo
" : PluginClean(!) - confirm (or auto-approve) removal of unused plugins
"
" see :h vundle for more details or wiki for FAQ
" NOTE: comments after Plugin commands are not allowed.
" Put your stuff after this line
Install Plugins
Launch vim and run
: PluginInstall
vim +PluginInstall +qall
3、官方vim-lang插件
Config vim file .vimrc,Add content bellow in bottom of the file
" 官方的插件
" Some Linux distributions set filetype in /etc/vimrc.
" Clear filetype flags before changing runtimepath to force Vim to
" reload them.
filetype off
filetype plugin indent off
set runtimepath+=$GOROOT/misc/vim
filetype plugin indent on
syntax on
autocmd FileType go autocmd BufWritePre Fmt
4、代碼補全的插件gocode
配置go的環境變量,比如我的配置,GOPATH變量是必須要配置的,PATH中必須把GOPATH的bin也添加進去,否則沒有自動提示,會提示找不到模式
export GOROOT=/usr/local/go
export GOPATH=/data/app/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Set up gocode
Then you need to get the appropriate version of the gocode, for 6g/8g/5g compiler you can do this:
go get -u github.com/nsf/gocode (-u flag for "update")
Configure vim in .vimrc file
Plugin 'nsf/gocode', {'rtp': 'vim/'}
Install Plugins
Launch vim and run
: PluginInstall
vim +PluginInstall +qall
寫一個helloword程序,輸入fmt后按C-xC-o如果能看到函數的聲明展示出來,說明安裝是正確的。
4、代碼跳轉提示godef
Set up godef
go get -v code.google.com/p/rog-go/exp/cmd/godef
go install -v code.google.com/p/rog-go/exp/cmd/godef
git clone ~/.vim/bundle/vim-godef
Configure vim in .vimrc file
Bundle 'dgryski/vim-godef'
Install Plugins
Launch vim and run
: PluginInstall
vim +PluginInstall +qall
5、代碼結構提示gotags
Set up gotags
go get -u github.com/jstemmer/gotags
Put the following configuration in your vimrc:
Bundle 'majutsushi/tagbar'
nmap :TagbarToggle
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'
\ }
命令模式下按在右邊就會顯示當前文件下的函數名,結構體名等等,光標放到相應的tag上,按回車可以快速跳到程序中的相應位置。
再次按會關閉tag窗口。
PS:本地的.vimrc的配置
" 插件管理器 vundle
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" alternatively, pass a path where Vundle should install plugins
"let path = '~/some/path/here'
"call vundle#rc(path)
" let Vundle manage Vundle, required
Plugin 'gmarik/vundle'
" The following are examples of different formats supported.
" Keep Plugin commands between here and filetype plugin indent on.
" scripts on GitHub repos
" Plugin 'tpope/vim-fugitive'
" Plugin 'Lokaltog/vim-easymotion'
" Plugin 'tpope/vim-rails.git'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
" Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" scripts from
" Plugin 'L9'
" Plugin 'FuzzyFinder'
" scripts not on GitHub
" Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
" Plugin ''
" ...
"
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
" filetype plugin on
"
" Brief help
" : PluginList - list configured plugins
" : PluginInstall(!) - install (update) plugins
" : PluginSearch(!) foo - search (or refresh cache first) for foo
" : PluginClean(!) - confirm (or auto-approve) removal of unused plugins
"
" see :h vundle for more details or wiki for FAQ
" NOTE: comments after Plugin commands are not allowed.
" Put your stuff after this line
syntax on
" ********************************************************************
" 這里省略了其它不相關的插件
" vimgdb插件
run macros/gdb_mappings.vim
" 官方的插件
" Some Linux distributions set filetype in /etc/vimrc.
" Clear filetype flags before changing runtimepath to force Vim to
" reload them.
filetype off
filetype plugin indent off
set runtimepath+=$GOROOT/misc/vim
filetype plugin indent on
syntax on
autocmd FileType go autocmd BufWritePre buffer Fmt
" 代碼補全的插件
Bundle 'Blackrush/vim-gocode'
" 代碼跳轉提示
Bundle 'dgryski/vim-godef'
" 代碼結構提示
Bundle 'majutsushi/tagbar'
nmap F8 :TagbarToggleCR
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語言頁面跳轉 go語言怎么運行
網站路徑:http://www.kartarina.com/article30/ddddoso.html
成都網站建設公司_創新互聯,為您提供網站營銷、小程序開發、關鍵詞優化、App開發、全網營銷推廣、企業網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯