您的位置 首页 golang

Go在windows下编译Linux可执行文件

前言

Go在windows下编译Linux可执行文件

最近楼主做了一个滑块验证码登录功能,但有个问题,悲观估计一天大约会产生两百多G临时图片放在fastdfs文件服务器上。

Go在windows下编译Linux可执行文件

所以楼主基于go(新手)的goroutine 多线程 写了一个清理程序: imgcleaner, 来缓解文件服务器压力 。本文将重点描述 Go在windows下编译 Linux 可执行文件全过程,分享给大家,希望对大家有所帮助。

作为go新手,最好的学习方式就是亲自动手加用笔记整理。

本文分为三部分:

  1. 第一部分 编译构建
  2. 第二部分 linux环境部署
  3. 第三部分 遇到问题及解决

第一部分 编译构建

在Goland ide中找到需要发布的程序入口文件:main.go

Go在windows下编译Linux可执行文件

打开ide terminal窗口

Go在windows下编译Linux可执行文件

设置编译linux 二进制文件 环境变量

Go在windows下编译Linux可执行文件

set CGO_ENABLED=0
set GOARCH=amd64
set GOOS=linux
 

构建

go build main.go
 

输出linux二进制可执行文件:main,如果觉得名字不直观,可以修改。比如楼主重命名为:imgcleaner,意为图片清理器。

Go在windows下编译Linux可执行文件

第二部分 linux环境部署

修改 imgcleaner 文件权限

chmod -R 777 imgcleaner
 

用nohup设置后台执行,同时设置输出日志文件:imgcleaner.log,配置参数文件:

-conf="/home/aiuap_cj/imgcleaner/conf.toml"
 

最终部署命令:

nohup ./imgcleaner -conf="/home/aiuap_cj/imgcleaner/conf.toml" ->imgcleaner.log &
 

效果

Go在windows下编译Linux可执行文件

第三部分 遇到问题及解决

问题1 unrecognized import path “golang.org/x/sys/ unix

Go在windows下编译Linux可执行文件

…. github .comSirupsenlogrusterminal_check_unix.go:5:8: cannot find package “golang.org/x/sys/unix” in any of:

C:Gosrcgolang.orgxsysunix (from $GOROOT)

D:gitgophersrcgolang.orgxsysunix (from $GOPATH)

解决

在src目录下,创建“golang.org/x”目录,然后打开cmd窗口,找到对应的目录,用git clone命令从下载相关文件。

Go在windows下编译Linux可执行文件

问题2 unrecognized import path “golang.org/x/*”的解决办法(网上找的)遇到的坑

先用git clone命令,克隆的代码到本地,然后把golang.org目录拷贝到src目录下。但是当用go build的时会报错。

syscall_linux.go:38:2: expected 'STRING' found 'return'
 

解决

留下 D:gitgophersrcgolang.orgxsys这个目录下的文件,其他都删除,再次用go build,问题解决。

Go在windows下编译Linux可执行文件

文章来源:智云一二三科技

文章标题:Go在windows下编译Linux可执行文件

文章地址:https://www.zhihuclub.com/100575.shtml

关于作者: 智云科技

热门文章

评论已关闭

1条评论

  1. Incredible! This blog looks just like my old one!
    It’s on a entirely different subject but it has pretty much the same page layout and
    design. Outstanding choice of colors!

网站地图