這些是死知識,把常用的記住,不常用的直接查表就行了

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名與空間、網(wǎng)絡(luò)空間、營銷軟件、網(wǎng)站建設(shè)、富錦網(wǎng)站維護、網(wǎng)站推廣。
golang 的fmt 包實現(xiàn)了格式化I/O函數(shù),類似于C的 printf 和 scanf。
type Human struct {
Name string
}
var people = Human{Name:"zhangsan"}
golang沒有 '%u' 點位符,若整數(shù)為無符號類型,默認(rèn)就會被打印成無符號的。
寬度與精度的控制格式以Unicode碼點為單位。寬度為該數(shù)值占用區(qū)域的最小寬度;精度為小數(shù)點之后的位數(shù)。
操作數(shù)的類型為int時,寬度與精度都可用字符 '*' 表示。
對于 %g/%G 而言,精度為所有數(shù)字的總數(shù),例如:123.45,%.4g 會打印123.5,(而 %6.2f 會打印123.45)。
%e 和 %f 的默認(rèn)精度為6
對大多數(shù)的數(shù)值類型而言,寬度為輸出的最小字符數(shù),如果必要的話會為已格式化的形式填充空格。
而以字符串類型,精度為輸出的最大字符數(shù),如果必要的話會直接截斷。
使用起來很簡單,一般配合fmt.Printf()使用,因為fmt的Printf()是有格式的輸出,切忌使用Println(),否則將會以字符串的形式輸出。
查看原文: golang fmt格式“占位符”
本教程介紹 Go 中多模塊工作區(qū)的基礎(chǔ)知識。使用多模塊工作區(qū),您可以告訴 Go 命令您正在同時在多個模塊中編寫代碼,并輕松地在這些模塊中構(gòu)建和運行代碼。
在本教程中,您將在共享的多模塊工作區(qū)中創(chuàng)建兩個模塊,對這些模塊進行更改,并在構(gòu)建中查看這些更改的結(jié)果。
本教程需要 go1.18 或更高版本。使用go.dev/dl中的鏈接確保您已在 Go 1.18 或更高版本中安裝了 Go 。
首先,為您要編寫的代碼創(chuàng)建一個模塊。
1、打開命令提示符并切換到您的主目錄。
在 Linux 或 Mac 上:
在 Windows 上:
2、在命令提示符下,為您的代碼創(chuàng)建一個名為工作區(qū)的目錄。
3、初始化模塊
我們的示例將創(chuàng)建一個hello依賴于 golang.org/x/example 模塊的新模塊。
創(chuàng)建你好模塊:
使用 . 添加對 golang.org/x/example 模塊的依賴項go get。
在 hello 目錄下創(chuàng)建 hello.go,內(nèi)容如下:
現(xiàn)在,運行 hello 程序:
在這一步中,我們將創(chuàng)建一個go.work文件來指定模塊的工作區(qū)。
在workspace目錄中,運行:
該go work init命令告訴為包含目錄中模塊的工作空間go創(chuàng)建一個文件 。go.work./hello
該go命令生成一個go.work如下所示的文件:
該go.work文件的語法與go.mod相同。
該go指令告訴 Go 應(yīng)該使用哪個版本的 Go 來解釋文件。它類似于文件中的go指令go.mod 。
該use指令告訴 Go在進行構(gòu)建時hello目錄中的模塊應(yīng)該是主模塊。
所以在模塊的任何子目錄中workspace都會被激活。
2、運行工作區(qū)目錄下的程序
在workspace目錄中,運行:
Go 命令包括工作區(qū)中的所有模塊作為主模塊。這允許我們在模塊中引用一個包,即使在模塊之外。在模塊或工作區(qū)之外運行g(shù)o run命令會導(dǎo)致錯誤,因為該go命令不知道要使用哪些模塊。
接下來,我們將golang.org/x/example模塊的本地副本添加到工作區(qū)。然后,我們將向stringutil包中添加一個新函數(shù),我們可以使用它來代替Reverse.
在這一步中,我們將下載包含該模塊的 Git 存儲庫的副本golang.org/x/example,將其添加到工作區(qū),然后向其中添加一個我們將從 hello 程序中使用的新函數(shù)。
1、克隆存儲庫
在工作區(qū)目錄中,運行g(shù)it命令來克隆存儲庫:
2、將模塊添加到工作區(qū)
該go work use命令將一個新模塊添加到 go.work 文件中。它現(xiàn)在看起來像這樣:
該模塊現(xiàn)在包括example.com/hello模塊和 `golang.org/x/example 模塊。
這將允許我們使用我們將在模塊副本中編寫的新代碼,而不是使用命令stringutil下載的模塊緩存中的模塊版本。
3、添加新功能。
我們將向golang.org/x/example/stringutil包中添加一個新函數(shù)以將字符串大寫。
將新文件夾添加到workspace/example/stringutil包含以下內(nèi)容的目錄:
4、修改hello程序以使用該功能。
修改workspace/hello/hello.go的內(nèi)容以包含以下內(nèi)容:
從工作區(qū)目錄,運行
Go 命令在go.work文件指定的hello目錄中查找命令行中指定的example.com/hello模塊 ,同樣使用go.work文件解析導(dǎo)入golang.org/x/example。
go.work可以用來代替添加replace 指令以跨多個模塊工作。
由于這兩個模塊在同一個工作區(qū)中,因此很容易在一個模塊中進行更改并在另一個模塊中使用它。
現(xiàn)在,要正確發(fā)布這些模塊,我們需要發(fā)布golang.org/x/example 模塊,例如在v0.1.0. 這通常通過在模塊的版本控制存儲庫上標(biāo)記提交來完成。發(fā)布完成后,我們可以增加對 golang.org/x/example模塊的要求hello/go.mod:
這樣,該go命令可以正確解析工作區(qū)之外的模塊。
1.先安裝Go對應(yīng)的開源Swagger相關(guān)的庫
go get?github.com/swaggo/swag/cmd/swag
go get github.com/swaggo/gin-swagger
go get?github.com/swaggo/files
go get?github.com/alecthomas/template
2.驗證是否安裝成功:swag -v
3.針對接口寫入注解
// @Summary 獲取多個標(biāo)簽
// @Tags 標(biāo)簽
// @Produce? json
// @Param name query string false "標(biāo)簽名稱" maxlength(100)
// @Param state query int false "狀態(tài)" Enums(0, 1) default(1)
// @Param page query int false "頁碼"
// @Param page_size query int false "每頁數(shù)量"
// @Success 200 {object} model.TagSwagger "成功"
// @Failure 400 {object} errcode.Error "請求錯誤"
// @Failure 500 {object} errcode.Error "內(nèi)部錯誤"
// @Router /api/v1/tags [get]
func (t Tag) List(c *gin.Context) {
}
// @Summary 新增標(biāo)簽
// @Tags 標(biāo)簽
// @Produce? json
// @Param name body string true "標(biāo)簽名稱" minlength(3) maxlength(100)
// @Param state body int false "狀態(tài)" Enums(0, 1) default(1)
// @Param created_by body string false "創(chuàng)建者" minlength(3) maxlength(100)
// @Success 200 {object} model.Tag "成功"
// @Failure 400 {object} errcode.Error "請求錯誤"
// @Failure 500 {object} errcode.Error "內(nèi)部錯誤"
// @Router /api/v1/tags [post]
func (t Tag) Create(c *gin.Context) {
}
// @Summary 更新標(biāo)簽
// @Tags 標(biāo)簽
// @Produce? json
// @Param id path int true "標(biāo)簽ID"
// @Param name body string false "標(biāo)簽名稱" minlength(3) maxlength(100)
// @Param state body int false "狀態(tài) (0為未刪除、1為已刪除)" Enums(0, 1) default(1)
// @Param modified_by body string true "修改者" minlength(3) maxlength(100)
// @Success 200 {array} model.Tag "成功"
// @Failure 400 {object} errcode.Error "請求錯誤"
// @Failure 500 {object} errcode.Error "內(nèi)部錯誤"
// @Router /api/v1/tags/{id} [put]
func (t Tag) Update(c *gin.Context) {
}
4.針對整個項目進行注解,直接在main方法寫入如下注解
//@title 項目名稱
//@version 1.0
//@description 這里是描述
func main() {
5.生成執(zhí)行 swag init
這時會在我項目的docs文件夾下面生成docs.go、swagger.json、swagger.yaml三個文件
6.要在routers中進行默認(rèn)初始化和注冊對應(yīng)的路由:
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
同時要引用 _"blog-service/docs" ,不然會報錯
7.查看接口文檔 :
8.ok,完成
go語言的官網(wǎng)是
Go語言是谷歌推出的一種全新的編程語言,可以在不損失應(yīng)用程序性能的情況下降低代碼的復(fù)雜性。谷歌首席軟件工程師羅布派克(Rob Pike)說:我們之所以開發(fā)Go,是因為過去10多年間軟件開發(fā)的難度令人沮喪。
Go是谷歌2009發(fā)布的第二款編程語言。2009年7月份,谷歌曾發(fā)布了Simple語言,它是用來開發(fā)Android應(yīng)用
Go Logo
的一種BASIC語言.
北京時間2010年1月10日,Go語言摘得了TIOBE公布的2009年年度大獎。該獎項授予在2009年市場份額增長最多的編程語言。
谷歌資深軟件工程師羅布·派克(Rob Pike)表示,“Go讓我體驗到了從未有過的開發(fā)效率。”派克表示,和今天的C++或C一樣,Go是一種系統(tǒng)語言。他解釋道,“使用它可以進行快速開發(fā),同時它還是一個真正的編譯語言,我們之所以現(xiàn)在將其開源,原因是我們認(rèn)為它已經(jīng)非常有用和強大。”
2007年,谷歌把Go作為一個20%項目開始研發(fā),即讓員工抽出本職工作之外時間的20%, 投入在該項目上。除了派克外,該項目的成員還有其他谷歌工程師也參與研發(fā)。
派克表示,編譯后Go代碼的運行速度與C語言非常接近,而且編譯速度非常快,就像在使用一個交互式語言。現(xiàn)有編程語言均未專門對多核處理器進行優(yōu)化。Go就是谷歌工程師為這類程序編寫的一種語言。它不是針對編程初學(xué)者設(shè)計的,但學(xué)習(xí)使用它也不是非常困難。Go支持面向?qū)ο螅揖哂姓嬲拈]包(closures)和反射 (reflection)等功能。
在學(xué)習(xí)曲線方面,派克認(rèn)為Go與Java類似,對于Java開發(fā)者來說,應(yīng)該能夠輕松學(xué)會 Go。之所以將Go作為一個開源項目發(fā)布,目的是讓開源社區(qū)有機會創(chuàng)建更好的工具來使用該語言,例如 Eclipse IDE中的插件。
在谷歌公開發(fā)布的所有網(wǎng)絡(luò)應(yīng)用中,均沒有使用Go,但是谷歌已經(jīng)使用該語言開發(fā)了幾個內(nèi)部項目。派克表示,Go是否會對谷歌即將推出的Chrome OS產(chǎn)生影響,還言之尚早,不過Go的確可以和Native Client配合使用。他表示“Go可以讓應(yīng)用完美的運行在瀏覽器內(nèi)。”例如,使用Go可以更高效的實現(xiàn)Wave,無論是在前端還是后臺。
Go 同時具有兩種編譯器,一種是建立在GCC基礎(chǔ)上的Gccgo,另外一種是分別針對64位x64和32位x86計算機的一套編譯器(6g和8g)。谷歌目前正在研發(fā)其對ARM芯片和Android設(shè)備的支持。派克表示,“Android手機存在的問題是,我們一直沒有一個數(shù)學(xué)協(xié)處理器。”
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
安裝
// 這里直接執(zhí)行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中執(zhí)行g(shù)db時,報 “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文件的開頭添加下面的內(nèi)容,有些不是必須的,可以注掉
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的環(huán)境變量,比如我的配置,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如果能看到函數(shù)的聲明展示出來,說明安裝是正確的。
4、代碼跳轉(zhuǎn)提示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、代碼結(jié)構(gòu)提示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'
\ }
命令模式下按在右邊就會顯示當(dāng)前文件下的函數(shù)名,結(jié)構(gòu)體名等等,光標(biāo)放到相應(yīng)的tag上,按回車可以快速跳到程序中的相應(yīng)位置。
再次按會關(guān)閉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
" ********************************************************************
" 這里省略了其它不相關(guān)的插件
" 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'
" 代碼跳轉(zhuǎn)提示
Bundle 'dgryski/vim-godef'
" 代碼結(jié)構(gòu)提示
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'
\ }
由于工作的契機,最近學(xué)習(xí)了下Gossip,以及go語言的實現(xiàn)版本HashiCorp/memberlist。網(wǎng)上有個最基本的memberlist使用的example,在下邊的鏈接中,感興趣可以按照文檔運行下感受感受。本文主要講解memberlist v0.1.5 的使用細節(jié)。
Gossip是最終一致性協(xié)議,是目前性能最好,容錯性最好的分布式協(xié)議。目前Prometheus的告警組件alertmanager、redis、s3、區(qū)塊鏈等項目都有使用Gossip。本文不介紹Gossip原理,大家自行谷歌。
簡單的幾步即可搭建gossip集群
感謝已經(jīng)有網(wǎng)友為我們實現(xiàn)了一個example(
)。
哪里有問題,還請大家多多指正
名稱欄目:包含v免簽go語言版的詞條
本文網(wǎng)址:http://chinadenli.net/article27/dsippcj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站收錄、、品牌網(wǎng)站設(shè)計、品牌網(wǎng)站建設(shè)、手機網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)