2020-09-22 15:11:09 +08:00
|
|
|
|
# goenv
|
|
|
|
|
golang environment
|
2020-09-22 16:39:22 +08:00
|
|
|
|
|
|
|
|
|
# docker
|
|
|
|
|
|
|
|
|
|
## 拉取镜像
|
|
|
|
|
```
|
2023-08-09 01:06:30 +08:00
|
|
|
|
docker pull ccr.ccs.tencentyun.com/xiaoqidun/goenv:latest
|
2020-09-22 16:39:22 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-09-22 16:45:17 +08:00
|
|
|
|
## 运行镜像
|
2020-09-22 16:39:22 +08:00
|
|
|
|
```
|
2023-08-09 01:06:30 +08:00
|
|
|
|
docker run -it --rm ccr.ccs.tencentyun.com/xiaoqidun/goenv
|
2020-09-22 16:39:22 +08:00
|
|
|
|
```
|
2020-09-22 16:45:17 +08:00
|
|
|
|
|
2020-09-23 14:22:42 +08:00
|
|
|
|
# 编译项目
|
|
|
|
|
|
|
|
|
|
## 将项目文件映射到容器内部
|
2020-09-23 14:29:05 +08:00
|
|
|
|
|
|
|
|
|
### linux - bash
|
|
|
|
|
```shell
|
2024-06-22 23:01:47 +08:00
|
|
|
|
docker run --rm -it -v $(pwd):/usr/src/app -w /usr/src/app ccr.ccs.tencentyun.com/xiaoqidun/goenv
|
2020-09-23 14:29:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### windows - cmd
|
2020-09-23 14:22:42 +08:00
|
|
|
|
```bat
|
2024-06-22 23:01:47 +08:00
|
|
|
|
docker run --rm -it -v %cd%:/usr/src/app -w /usr/src/app ccr.ccs.tencentyun.com/xiaoqidun/goenv
|
2020-09-23 14:22:42 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-09-23 14:41:55 +08:00
|
|
|
|
### windows - powershell
|
|
|
|
|
```powershell
|
2024-06-22 23:01:47 +08:00
|
|
|
|
docker run --rm -it -v "${pwd}:/usr/src/app" -w /usr/src/app ccr.ccs.tencentyun.com/xiaoqidun/goenv
|
2020-09-23 14:41:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-09-23 14:22:42 +08:00
|
|
|
|
## 在容器内执行命令进行编译
|
2020-09-23 15:04:16 +08:00
|
|
|
|
|
2020-09-23 15:10:06 +08:00
|
|
|
|
### 编译linux程序
|
2020-09-23 14:22:42 +08:00
|
|
|
|
```shell
|
2020-09-23 15:11:14 +08:00
|
|
|
|
export GOOS=linux
|
|
|
|
|
export GOARCH=amd64
|
|
|
|
|
export CGO_ENABLED=0
|
2020-09-23 15:10:06 +08:00
|
|
|
|
go build -o main_linux_amd64 main.go
|
2020-09-23 15:00:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
2020-09-23 15:04:16 +08:00
|
|
|
|
### 编译android程序
|
|
|
|
|
```shell
|
|
|
|
|
export CC=aarch64-linux-android21-clang
|
|
|
|
|
export GOOS=android
|
|
|
|
|
export GOARCH=arm64
|
|
|
|
|
export CGO_ENABLED=1
|
|
|
|
|
go build -o main_android_arm64 main.go
|
|
|
|
|
```
|
|
|
|
|
|
2020-09-23 15:10:06 +08:00
|
|
|
|
### 编译windows程序
|
|
|
|
|
```shell
|
|
|
|
|
export GOOS=windows
|
|
|
|
|
export GOARCH=amd64
|
|
|
|
|
export CGO_ENABLED=0
|
|
|
|
|
go build -o main_windows_amd64.exe main.go
|
|
|
|
|
```
|
|
|
|
|
|
2020-09-23 15:00:39 +08:00
|
|
|
|
# 编译说明
|
|
|
|
|
|
|
|
|
|
## GOOS
|
|
|
|
|
> 用于标识编译的目标操作系统
|
|
|
|
|
|
|
|
|
|
## GOARCH
|
|
|
|
|
> 用于标识编译的目标系统架构
|
|
|
|
|
|
|
|
|
|
## CGO_ENABLED
|
|
|
|
|
- 0 静态编译,禁用CGO,适用于android和darwin(apple)外的目标操作系统
|
|
|
|
|
- 1 动态编译,使用CGO,适用于依靠外部工具链才能完成编译的目标操作系统
|
|
|
|
|
|
|
|
|
|
## 动态编译
|
|
|
|
|
- 动态编译必须设置CC变量,指定C编译器可执行文件路径或者在PATH变量内的可执行C编译器名称
|