在CentOS系统中,使用Golang进行打包的命令主要有以下几个:
基本命令:
go build -o <output_file> <source_file.go>
或者针对整个包:
go build -o <output_file> .
指定Go版本(如果系统中安装了多个Go版本):
go1.x build -o <output_file> <source_file.go>
GOOS=<target_os> GOARCH=<target_arch> go build -o <output_file> <source_file.go>
例如,编译为Windows 64位可执行文件:GOOS=windows GOARCH=amd64 go build -o myapp.exe main.go
CGO_ENABLED=0 GOOS=<target_os> GOARCH=<target_arch> go build -a -installsuffix cgo -o <output_file> <source_file.go>
例如,静态编译为Linux 64位可执行文件:CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o myapp_linux_amd64 main.go
gox进行多平台打包安装gox:
go get github.com/mitchellh/gox
运行gox:
gox -osarch="linux/amd64 windows/amd64 darwin/amd64" -output="myapp_{{.OS}}_{{.Arch}}"
packr打包资源文件安装packr:
go get github.com/gobuffalo/packr/v2/packr2
在代码中使用packr:
import (
"github.com/gobuffalo/packr/v2"
)
func main() {
box := packr.New("MyBox", "./templates")
template, _ := box.FindString("index.html")
fmt.Println(template)
}
打包资源文件:
packr2 build
go-bindata打包资源文件安装go-bindata:
go get -u github.com/go-bindata/go-bindata/...
生成绑定文件:
go-bindata -o=bindata.go -pkg=main ./templates/...
在代码中使用go-bindata:
import (
"github.com/go-bindata/go-bindata"
)
func main() {
data, err := bindata.Asset("templates/index.html")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}
GOPATH和GOROOT环境变量。通过这些命令,你可以在CentOS系统上灵活地进行Golang项目的打包和部署。