golang 在 Mac、Linux、Window 下交叉编译

目录

  • 交叉编译
    • 参数说明
      • CGO_ENABLED
      • [GOOS : 目标操作系统【darwin、linux、windows】](#GOOS : 目标操作系统【darwin、linux、windows】)
      • [GOARCH :目标架构【386、amd64、arm】](#GOARCH :目标架构【386、amd64、arm】)
    • [Mac 下编译,Linux / Windows 下执行](#Mac 下编译,Linux / Windows 下执行)
    • [Linux 下编译,Mac / Windows 下执行](#Linux 下编译,Mac / Windows 下执行)
    • [Windows 下编译,Mac / Linux 下执行](#Windows 下编译,Mac / Linux 下执行)

Golang的交叉编译是指将Go程序从一个操作系统和架构编译为另一个操作系统和架构的过程。

也就是说我们可以在一个开发环境中编写代码,然后可以为多个不同的目标平台生成可执行文件。

交叉编译

参数说明

CGO_ENABLED

CGO 表示golang中的工具,CGO_ENABLED 表示CGO禁用,交叉编译中不能使用CGO

GOOS : 目标操作系统【darwin、linux、windows】

  • mac:darwin
  • linux:linux
  • windows:windows

GOARCH :目标架构【386、amd64、arm】

  • 386:也称 x86,对应 32 位操作系统
  • amd64:也称 x64,对应 64 位操作系统
  • arm:这种架构一般用于嵌入式开发。比如 Android、IOS、Win mobile、TIZEN 等

Mac 下编译,Linux / Windows 下执行

bash 复制代码
# Linux 下执行
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go
# Windows 下执行
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

Linux 下编译,Mac / Windows 下执行

bash 复制代码
# Mac 下执行
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go
# Windows 下执行
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

Windows 下编译,Mac / Linux 下执行

需要写一个批处理程序,在里面去设置,因为 Windows 下的 terminal 不支持shell

bash 复制代码
# Mac 下执行
SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build main.go
bash 复制代码
# Linux 下执行
SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build main.go
相关推荐
一位摩羯座DBA2 小时前
Redhat&Centos挂载镜像
linux·运维·centos
学习3人组2 小时前
CentOS配置网络
linux·网络·centos
weixin_307779132 小时前
Hive集群之间迁移的Linux Shell脚本
大数据·linux·hive·bash·迁移学习
漫步企鹅3 小时前
【蓝牙】Linux Qt4查看已经配对的蓝牙信息
linux·qt·蓝牙·配对
cui_win3 小时前
【网络】Linux 内核优化实战 - net.core.flow_limit_table_len
linux·运维·网络
梦在深巷、3 小时前
MySQL/MariaDB数据库主从复制之基于二进制日志的方式
linux·数据库·mysql·mariadb
冰橙子id4 小时前
linux系统安全
linux·安全·系统安全
stark张宇4 小时前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
Johny_Zhao4 小时前
Ubuntu系统安装部署Pandawiki智能知识库
linux·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm·pandawiki
悲伤小伞4 小时前
linux_git的使用
linux·c语言·c++·git