menu Chancel's blog
rss_feed
Chancel's blog
时来天地皆同力

GO全平台交叉编译工具GoReleaser

作者:Chancel Yang, 时间:2022 Jun 10, 阅读:79

经常可以看到使用GO语言写的程序发布如下程序包

DeepinScreenshot_select-area_20220610110124.png

这是基于GoReleaser进行打包

各个平台的安装方法可参考goreleaser.com - 官方文档

以下以Ubuntu1804为例,打包一个GO程序

首先是安装GoReleaser

echo 'deb [trusted=yes] https://repo.goreleaser.com/apt/ /' | sudo tee /etc/apt/sources.list.d/goreleaser.list
sudo apt update
sudo apt install goreleaser

安装完成后,在项目中运行goreleaser init命令自动生成goreleaser配置单,如下

➜  wallhaven git:(master) ✗ goreleaser init  
   • Generating .goreleaser.yml file
   • config created; please edit accordingly to your needs file=.goreleaser.yml

配置单生成后,就可以使用goreleaser --snapshot --skip-publish --rm-dist进行打包,如下

➜  wallhaven git:(master) ✗ goreleaser --snapshot --skip-publish --rm-dist
   • releasing...     
   • loading config file       file=.goreleaser.yml
   • loading environment variables
   • getting and validating git state
      • ignoring errors because this is a snapshot error=git doesn't contain any tags. Either add a tag or use --snapshot
      • building...               commit=b8eb2bc2d7daeb959161679d01c76393fea29c77 latest tag=v0.0.0
      • pipe skipped              error=disabled during snapshot mode
   • parsing tag      
   • setting defaults 
      • snapshotting     
...

如需自定义设置可修改.goreleaser.yml配置单内容

最后再说说自带的打包方式,如果需要单独交叉编译可参考如下方法

CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go

GOOS参数是面向平台,参考值

  • darwin:MacOS
  • windows: Windows
  • linux: Linux

GOARCH参数是面向架构,参考值

  • amd64
  • 386
  • arm64
  • mipsle
  • mipsle64
  • mipsle64le

[[replyMessage== null?"发表评论":"发表评论 @ " + replyMessage.m_author]]

account_circle
email
web_asset
textsms

评论列表([[messageResponse.total]])

还没有可以显示的留言...
[[messageItem.m_author]] [[messageItem.m_author]]
[[messageItem.create_time]]
[[getEnviron(messageItem.m_environ)]]
[[subMessage.m_author]] [[subMessage.m_author]] @ [[subMessage.parent_message.m_author]] [[subMessage.parent_message.m_author]]
[[subMessage.create_time]]
[[getEnviron(messageItem.m_environ)]]