Monday, March 14, 2022

[SOLVED] Issues while compiling a Go app from Windows to Linux

Issue

My issue:

I need to compile a Go application from Windows (current workspace) to Linux (deployment environment), I've try several times using few techniques described in this platform and many more and nothing help, everything looks fine but isn't work.

I'm compiling for linux using this script in windows

$ set GOARCH=amd64
$ set GOOS=linux
$ go build app_name.go

in the cmd folder I can find a file named app_name with no extension, which should be fine. Then, I receive an error when trying to execute the binary in Linux, using few ways

$ ./app_name
-bash: ./app_name: Permission denied

$ source app_name
-bash: source: app_name: cannot execute binary file

this is the file that I received after the compilation:

-rw-r--r-- 1 xxx  xxx  7313830 Jan 26 16:59 app_name

If I run the application in windows with go run -arguments app_name.go it works perfect, but the issue appear after compile or try to compile the app for linux, I can't run the app even as root


Solution

On Unix systems, a file must have the executable bit set in order to be able to be run. This is the only way that the system knows what programs or scripts can be run, and differs from Windows, which uses extensions.

In this case, you need to change permissions by running chmod u+x unified_mapper before trying to execute it. If you want other users to execute it as well, you can do something like chmod 755 unified_mapper. Run man 1 chmod in the manual page to see more details about possible invocations.



Answered By - bk2204
Answer Checked By - Mary Flores (WPSolving Volunteer)