1. Golang 编译命令行
go get

“go get” command is the standard way of downloading and installing packages and related dependencies.

-v-u-d-x-xgo get
-d-f-u-fix-insecure-t-u
go build
go build.go
$ go build hello.go
$ lshello hello.go
-o
$ go build -o mygo hello.go
$ lsmygo hello.go
go build
$ go build
$ lsgo-examples hello.go

1.2.1. go build 不同系统

Golang 支持在一个平台下生成另一个平台可执行程序的交叉编译功能。

Mac 下编译 Linux, Windows 平台的 64 位可执行程序:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build test.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build test.go

Linux 下编译 Mac, Windows 平台的 64 位可执行程序:

CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build test.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build test.go

Windows 下编译 Mac, Linux 平台的 64 位可执行程序:

SET CGO_ENABLED=0
SET GOOS=darwin3
SET GOARCH=amd64
go build test.go

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build test.go
  • GOOS: 目标可执行程序运行操作系统, 支持 darwin, freebsd, linux, windows
  • GOARCH: 目标可执行程序操作系统构架, 包括 386, amd64, arm

Golang version 1.5 以前版本在首次交叉编译时还需要配置交叉编译环境:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash
go install

与 build 命令相比, install 命令在编译源码后还会将可执行文件或库文件安装到约定的目录下。

go install
go installsrcbinbingo install
go installsrcpkg