Rachel
Rachel

Reputation: 13

Cross compile Go binaries on Macbook Pro 2019 for Amazon AWS EC2

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):

enter image description here

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

enter image description here

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:

enter image description here

Upvotes: 0

Views: 567

Answers (3)

user1934428
user1934428

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

Allan Jacquet
Allan Jacquet

Reputation: 11

I think you should test without this $ before GOOS

sudo GOOS=linux GOARCH=amd64 go build -o main

Upvotes: 1

Russell Smith
Russell Smith

Reputation: 163

Remove the $ in front of GOOS.

GOOS=linux GOARCH=amd64 go build -o main

Upvotes: 2

Related Questions