《Go 语言程序设计》(Mark Summerfield)电子书网盘下载免费在线阅读
资源链接:
链接:https://pan.baidu.com/s/1SVbBaPldaVXv49op6H6klw
提取码:br6t书名:Go 语言程序设计
作者:Mark Summerfield
译者:许式伟
豆瓣评分:6.9
出版社:人民邮电出版社
出版年份:2013-8-1
页数:354
内容简介:
国外最经典的Go语言著作,Go语言编程的先驱者Mark Summerfield的实践经验总结。
这是一本Go语言实战指南,帮你了解Go语言,按Go语言的方式思考,以及使用Go语言来编写高性能软件。
作者展示了如何编写充分利用Go语言突破性的特性和惯用法的代码,以及Go语言在其他语言之上所做的改进,并着重强调了Go语言的关键创新。
注重实践教学,每章都提供了多个经过精心设计的代码示例。
由国内第一个核心服务完全采用Go语言实现的团队——七牛团队核心成员翻译。
作者简介:
Mark Summerfield Qtrac公司创始人,独立的培训讲师、顾问、技术编辑,Go、Python、C++、Qt和PyQt方面的技术作家。他的著作包括Rapid GUI Programming with Python and Qt、C++ GUI Programming with Qt 4(与Jasmin Blanchette合著)、Programming in Python 3和Advanced Qt Programming等。
许式伟——七牛云存储CEO,开源爱好者,发布过十余个C++开源项目,拥有超过15年的C/C++开发经验。
吕桂华——七牛云存储联合创始人,拥有十余年的C/C++大型项目开发经验,也曾在Java和.NET平台上探索多年。
徐 立——七牛云存储首席布道师,前盛大创新院高级研究员。
何李石——七牛云存储布道师。
七牛云存储技术团队是国内第一个核心服务完全采用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 https://github.com/gmarik/vundle.git ~/.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 http://vim-scripts.org/vim/scripts.html
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 'file:///home/gmarik/path/to/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-x><C-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 https://github.com/dgryski/vim-godef ~/.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 http://vim-scripts.org/vim/scripts.html
" 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 'file:///home/gmarik/path/to/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>:TagbarToggle<CR>
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 中模糊测试的基础知识。通过模糊测试,随机数据会针对您的测试运行,以尝试找出漏洞或导致崩溃的输入。可以通过模糊测试发现的一些漏洞示例包括 SQL 注入、缓冲区溢出、拒绝服务和跨站点脚本攻击。
在本教程中,您将为一个简单的函数编写一个模糊测试,运行 go 命令,并调试和修复代码中的问题。
首先,为您要编写的代码创建一个文件夹。
1、打开命令提示符并切换到您的主目录。
在 Linux 或 Mac 上:
在 Windows 上:
2、在命令提示符下,为您的代码创建一个名为 fuzz 的目录。
3、创建一个模块来保存您的代码。
运行go mod init命令,为其提供新代码的模块路径。
接下来,您将添加一些简单的代码来反转字符串,稍后我们将对其进行模糊测试。
在此步骤中,您将添加一个函数来反转字符串。
a.使用您的文本编辑器,在 fuzz 目录中创建一个名为 main.go 的文件。
独立程序(与库相反)始终位于 package 中main。
此函数将接受string,使用byte进行循环 ,并在最后返回反转的字符串。
此函数将运行一些Reverse操作,然后将输出打印到命令行。这有助于查看运行中的代码,并可能有助于调试。
e.该main函数使用 fmt 包,因此您需要导入它。
第一行代码应如下所示:
从包含 main.go 的目录中的命令行,运行代码。
可以看到原来的字符串,反转它的结果,然后再反转它的结果,就相当于原来的了。
现在代码正在运行,是时候测试它了。
在这一步中,您将为Reverse函数编写一个基本的单元测试。
a.使用您的文本编辑器,在 fuzz 目录中创建一个名为 reverse_test.go 的文件。
b.将以下代码粘贴到 reverse_test.go 中。
这个简单的测试将断言列出的输入字符串将被正确反转。
使用运行单元测试go test
接下来,您将单元测试更改为模糊测试。
单元测试有局限性,即每个输入都必须由开发人员添加到测试中。模糊测试的一个好处是它可以为您的代码提供输入,并且可以识别您提出的测试用例没有达到的边缘用例。
在本节中,您将单元测试转换为模糊测试,这样您就可以用更少的工作生成更多的输入!
请注意,您可以将单元测试、基准测试和模糊测试保存在同一个 *_test.go 文件中,但对于本示例,您将单元测试转换为模糊测试。
在您的文本编辑器中,将 reverse_test.go 中的单元测试替换为以下模糊测试。
Fuzzing 也有一些限制。在您的单元测试中,您可以预测Reverse函数的预期输出,并验证实际输出是否满足这些预期。
例如,在测试用例Reverse("Hello, world")中,单元测试将返回指定为"dlrow ,olleH".
模糊测试时,您无法预测预期输出,因为您无法控制输入。
但是,Reverse您可以在模糊测试中验证函数的一些属性。在这个模糊测试中检查的两个属性是:
(1)将字符串反转两次保留原始值
(2)反转的字符串将其状态保留为有效的 UTF-8。
注意单元测试和模糊测试之间的语法差异:
(3)确保新包unicode/utf8已导入。
随着单元测试转换为模糊测试,是时候再次运行测试了。
a.在不进行模糊测试的情况下运行模糊测试,以确保种子输入通过。
如果您在该文件中有其他测试,您也可以运行go test -run=FuzzReverse,并且您只想运行模糊测试。
b.运行FuzzReverse模糊测试,查看是否有任何随机生成的字符串输入会导致失败。这是使用go test新标志-fuzz执行的。
模糊测试时发生故障,导致问题的输入被写入将在下次运行的种子语料库文件中go test,即使没有-fuzz标志也是如此。要查看导致失败的输入,请在文本编辑器中打开写入 testdata/fuzz/FuzzReverse 目录的语料库文件。您的种子语料库文件可能包含不同的字符串,但格式相同。
语料库文件的第一行表示编码版本。以下每一行代表构成语料库条目的每种类型的值。由于 fuzz target 只需要 1 个输入,因此版本之后只有 1 个值。
c.运行没有-fuzz标志的go test; 新的失败种子语料库条目将被使用:
由于我们的测试失败,是时候调试了。