这是本节的多页打印视图。 点击此处打印.

返回本页常规视图.

Golang 工具

Golang 命令行工具

1 - go命令行

go命令行

1.1 - 使用go

go命令行的使用

go是golang安装后自带的命令行工具:

$ which go
/usr/local/go/bin/go

go build

通过 build 命令执行编译工作:

go build hello.go

在不包含文件名时, go 工具会默认使用当前目录来编译。

go build

可以直接指定包:

go build github.com/goinaction/code/chapter3/wordcount

可以在编译时使用竞争检测器标志来编译程序:

go build -race

go run

go run 命令先编译,然后执行编译创建的可执行程序。

go run main.go

必须指定go文件,不能为空或者给参数".",这里和 go build 命令不同。

编译出来的可执行文件并不会保存,在执行完成后,目录中没有像 go build 命令那样生成一个可执行文件。

go clean

go clean 命令执行清理工作。

go vet

go vet 命令会帮开发人员检测代码的常见错误。

func main() {
	fmt.Printf("The quick brown fox jumped over lazy dogs", 3.14)
}

这段代码,可以编译通过,执行时也不会报错,但是,因为缺少格式化参数,所以结果会不符合预期:

The quick brown fox jumped over lazy dogs%!(EXTRA float64=3.14)

每次对代码先执行 go vet 再将其签入源代码库是一个很好的习惯。

go fmt

go fmt 命令做代码自动格式化并保存,会将代码修改成和 Go 源代码类似的风格。

go doc

在终端上可以直接使用 go doc 命令来打印文档。

go doc net

无需离开终端,即可快速浏览命令或者包的帮助。

godoc

在终端会话中输入如下命令:

godoc -http=:6060

在端口 6060 启动 Web 服务器。用浏览器打开 http://localhost:6060 可以看到一个页面,包含所有 Go 标准库和 GOPATH 下的 Go 源代码的文档。

2 - gdb

Golang gdb 工具

2.1 - gdb概述

Golang gdb 工具概述

参考资料

相关文章

2.2 - 安装gdb

Golang gdb 工具的安装

下载

https://www.gnu.org/software/gdb/download/

http://ftp.gnu.org/gnu/gdb

下载最新版本,如 gdb-9.2.tar.gz。

从源码构建

参照 README 文档的说明:

mkdir build
cd build

../configure 
make
make install

安装完成之后,验证一下:

$ which gdb
/usr/local/bin/gdb

$ gdb --version
GNU gdb (GDB) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

$ which gcore
/usr/bin/gcore

备注:

Mac下codesign的注意事项

在启动台搜索“钥匙串”,打开“钥匙串访问”,“证书助理” -》“创建证书”

Mac下build时需要注意的事项

在mac下可能会遇到如下问题:

$ go build test.go
$ gdb ./test
GNU gdb (GDB) 9.2
This GDB was configured as "x86_64-apple-darwin19.0.0".
......

Reading symbols from ./test...
(No debugging symbols found in ./test)

据说出现这个问题的原因是:

In Go 1.11, the debug information is compressed for purpose of reduce binary size, and gdb on the Mac does not understand compressed DWARF.

在Go 1.11中,为了减少二进制文件的大小,调试信息被压缩了,而Mac上的gdb不理解压缩后的DWARF,所以也可以指定-ldflags=-compressdwarf=false来解决。

The workaround is to also specify -ldflags=-compressdwarf=false which does exactly what it claims.

解决办法是指定-ldflags=-compressdwarf=false,这样就可以实现它的要求。

注意在mac 下build golang代码时需要添加 -ldflags=-compressdwarf=false 参数:

$ go build -ldflags=-compressdwarf=false test.go 
$ gdb ./test
GNU gdb (GDB) 9.2
This GDB was configured as "x86_64-apple-darwin19.0.0".
......

Reading symbols from ./test...
Loading Go Runtime support.

或者添加环境变量

export GOFLAGS="-ldflags=-compressdwarf=false"

参考资料:

3 - go callvis

callvis 以图片的形式展示 go 程序的调用关系

3.1 - go callvis概述

go callvis工具概述

介绍

go-callvis 以图片的形式展示了go程序的调用关系,这个工具在看复杂项目时尤其有用。

参考资料

相关文章

3.2 - 安装gdb

Golang gdb 工具的安装

准备

参考 https://github.com/ofabry/go-callvis 的要求,需要先准备:

因此需要先安装 graphviz,否则运行时会报错找不到"dot"。

  • mac下:直接运行 brew install graphviz

安装

运行命令,注意GOPATH 要正确设置好:

go get -u github.com/ofabry/go-callvis

验证

参考官方例子:https://github.com/ofabry/go-callvis/tree/master/examples

4 - go dep

官方的依赖解决和管理方案

4.1 - go dep概述

go dep工具概述

4.2 - 安装gdb

Golang dep 工具的安装

官方参考文档:

https://golang.github.io/dep/docs/installation.html

Linux下安装

执行下面的安装脚本:

$> curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh

Warning: the following project(s) have [[constraint]] stanzas in Gopkg.toml:

  ✗  github.com/deckarep/golang-set
  ✗  github.com/julienschmidt/httprouter
  ✗  github.com/magiconair/properties
  ✗  github.com/nu7hatch/gouuid

However, these projects are not direct dependencies of the current project:
they are not imported in any .go files, nor are they in the 'required' list in
Gopkg.toml. Dep only applies [[constraint]] rules to direct dependencies, so
these rules will have no effect.

Either import/require packages from these projects so that they become direct
dependencies, or convert each [[constraint]] to an [[override]] to enforce rules
on these projects, if they happen to be transitive dependencies,

由于github直接下载速度不快而且有时被墙,因此最好先翻墙,设置好http_proxy等环境变量再执行命令。