Golang中的常用命令

Go命令行有常用的这几个命令:

  • go build 编译包和依赖
  • go run 编译和运行go程序
  • go install 编译并安装包和依赖
  • go get 将依赖下载到当前依赖并安装

go help来看都可以编译构建,

go help

对于它们之间的区别,刚刚上手的同学可能比较容易混淆,下面我们就通过思维导图的方式来看看这些命令的异同。
image.png

go build

go build是这几个命令的基础,go build <命令源码文件/.>会生成目标系统的可执行文件

image.png

或生成与目录同名的可执行文件
image.png

go build对库源码文件的操作意义就不是很大了,不管是在项目目录下还是GOPATH目录下都不会生成任何文件,只是用来检查代码是否存在编译错误。
image.png

运行go build命令时加入标记-x,这样可以看到go build命令具体都执行了哪些操作,查看具体构建过程

go build -x hello_main.go 
WORK=/var/folders/js/pmxyyt3s2693n8_dk137t8l80000gn/T/go-build580263953
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile command-line-arguments=/Users/XXX/Library/Caches/go-build/66/66f8de4622b8ffa394c623ca185ac616e298e56b0922df729b6a595f48eafac7-d
packagefile fmt=/usr/local/go/pkg/darwin_amd64/fmt.a
...
packagefile internal/race=/usr/local/go/pkg/darwin_amd64/internal/race.a
EOF
mkdir -p $WORK/b001/exe/
cd .
/usr/local/go/pkg/tool/darwin_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=dvpJi7I5grdSogBnCMbW/lvYsRrpuUSUL86UZI79H/GF71-1TyEJxjFQJjlqbi/dvpJi7I5grdSogBnCMbW -extld=clang /Users/XXX/Library/Caches/go-build/66/66f8de4622b8ffa394c623ca185ac616e298e56b0922df729b6a595f48eafac7-d
/usr/local/go/pkg/tool/darwin_amd64/buildid -w $WORK/b001/exe/a.out # internal
mv $WORK/b001/exe/a.out hello_main
rm -r $WORK/b001/

go run

go run命令编译构建后直接运行,不会在项目目录或GOPATH目录下生成可执行文件。
传入-x参数查看详细执行过程可以看出对比go buildgo run省略了mv $WORK/b001/exe/a.out hello_main搬移可执行文件,而是直接执行输出结果。

➜  cli go run -x hello_main.go
WORK=/var/folders/js/pmxyyt3s2693n8_dk137t8l80000gn/T/go-build971127258
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile command-line-arguments=/Users/XXX/Library/Caches/go-build/71/719f02b57ed687b008c3f0febd1e1153726dca1999dd7c4f88adbbebfa82193c-d
packagefile fmt=/usr/local/go/pkg/darwin_amd64/fmt.a
...
packagefile internal/race=/usr/local/go/pkg/darwin_amd64/internal/race.a
EOF
mkdir -p $WORK/b001/exe/
cd .
/usr/local/go/pkg/tool/darwin_amd64/link -o $WORK/b001/exe/hello_main -importcfg $WORK/b001/importcfg.link -s -w -buildmode=exe -buildid=7eLhf19h12gDZOehA2Ps/E-LrBMzTVfVUHX0egkFE/HKms5XQlG7vZXdMiphwE/7eLhf19h12gDZOehA2Ps -extld=clang /Users/XXX/Library/Caches/go-build/71/719f02b57ed687b008c3f0febd1e1153726dca1999dd7c4f88adbbebfa82193c-d
$WORK/b001/exe/hello_main
Hello Lissdy!

所以不能直接单独运行的库源码文件不支持该命令。


库源码文件不支持go run

go install

go install大概等于go build+安装,所谓安装就是将命令源码生成的可执行文件或者库源码文件生成的归档文件放入GOPATH/bin或GOPATH/pkg中。
go install可执行文件从打印的详细步骤可以看出主要区别就是mv $WORK/b001/exe/a.out /Users/XXX/go/bin/hello_main,将可执行文件放入GOPATH/bin而不是当前目录下。

➜  cli go install -x hello_main.go 
WORK=/var/folders/js/pmxyyt3s2693n8_dk137t8l80000gn/T/go-build354803648
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile command-line-arguments=/Users/XXX/Library/Caches/go-build/66/66f8de4622b8ffa394c623ca185ac616e298e56b0922df729b6a595f48eafac7-d
packagefile fmt=/usr/local/go/pkg/darwin_amd64/fmt.a
...
packagefile internal/race=/usr/local/go/pkg/darwin_amd64/internal/race.a
EOF
mkdir -p $WORK/b001/exe/
cd .
/usr/local/go/pkg/tool/darwin_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=dvpJi7I5grdSogBnCMbW/lvYsRrpuUSUL86UZI79H/GF71-1TyEJxjFQJjlqbi/dvpJi7I5grdSogBnCMbW -extld=clang /Users/XXX/Library/Caches/go-build/66/66f8de4622b8ffa394c623ca185ac616e298e56b0922df729b6a595f48eafac7-d
/usr/local/go/pkg/tool/darwin_amd64/buildid -w $WORK/b001/exe/a.out # internal
mkdir -p /Users/XXX/go/bin/
mv $WORK/b001/exe/a.out /Users/XXX/go/bin/hello_main
rm -r $WORK/b001/

对于库源码文件来说,当module-aware模式打开时,执行go install只会构建缓存结果,并不会将结果安装到GOPATH/pkg。

image.png

go get

命令go get会自动从一些主流公用代码仓库(比如 GitHub)下载目标代码包,并把它们安装到环境变量GOPATH相应目录中。


image.png

参考:https://golang.google.cn/cmd/go/

你可能感兴趣的:(Golang中的常用命令)