Reputation: 13
I need to compile Go binaries on Macbook Pro 2019 for Amazon AWS EC2 Linux instance. Here is the screenshot (I searched at many places I couldn't get an answer for it):
I tried this command on my MacBook pro:
sudo $GOOS=linux GOARCH=amd64 go build -o main
and I get the error:
zsh: linux not found
I wrote this command to get a list of platforms it can cross-compile to like this and you can see there is an option for Linux too but it throws the error:
Upvotes: 0
Views: 567
Reputation: 22217
While the answers by Russell Smith and Allan Jacquet correctly explain how to do it correctly, they don't explain, why you got exactly that error message.
You were bitten by two zsh properties working together: Parameter expansion, and (not so obvious) '='-expansion.
In your command
sudo $GOOS=linux GOARCH=amd64 go build -o main
zsh first substitutes the parameters. You have one parameter substitution here, $GDOS
, and zsh replaces this with the content of the variable GDOS
. Assuming this variable does not exist at this point or is empty, this results in the line
sudo =linux GOARCH=amd64 go build -o main
Next step is "="-expansion, since the second word in this line starts with an equal-sign. In general, a word =something
searches a program named something in the PATH and replaces that word by the full path name. You can try this feature by doing for instance a echo =cat
(which, on my platform, outputs /usr/bin/cat
).
Since you don't have a program named linux
in your PATH, the command fails.
BTW, if you had enabled
set -u
in your shell, zsh would complained that you are using a variable GDOS which does not have a value yet; this would have made it obvious for you to find the error.
Upvotes: 0
Reputation: 11
I think you should test without this $ before GOOS
sudo GOOS=linux GOARCH=amd64 go build -o main
Upvotes: 1
Reputation: 163
Remove the $ in front of GOOS.
GOOS=linux GOARCH=amd64 go build -o main
Upvotes: 2