<p>It seems that <code>docker build</code> opens a new shell and <code>export</code> is needed. The following command created a Linux binary:</p>

<pre><code>export GOOS=linux; go build hello-world.go
</code></pre>

CC BY-SA 3.0
2017-08-06T20:05:37.597Z
2017-08-06T20:05:37.597Z
030
<p>Windows also has a one-liner solution, just a bit uglier than the linux version. Any of the below lines will do the job:</p>

<pre><code>cmd /C "SET GOOS=linux&amp;&amp; SET GOARCH=amd64&amp;&amp; go build main.go"
cmd /C "SET "GOOS=linux" &amp;&amp; SET "GOARCH=amd64" &amp;&amp; go build main.go"
</code></pre>

<p>Be careful, if you just write <code>...SET GOOS=linux &amp;&amp; SET ...</code> then you will get an error message </p>

<pre><code>cmd/go: unsupported GOOS/GOARCH pair linux /amd64
</code></pre>

<p>because there will be an extra space after the value <code>linux</code>. In order to avoid this, use either the condensed form <code>linux&amp;&amp;</code> or the other version with quotation marks.</p>

CC BY-SA 4.0
2020-02-21T13:58:59.420Z
2020-02-21T13:58:59.420Z
Csongor Halmai
<p>You can easily set the target operating system and processor architecture using the environment variables <strong>GOOS</strong> and <strong>GOARCH</strong> respectively. So, as you want to build it for linux operating system, following command with above environment variables will do, </p>

<pre><code>$ GOOS=linux GOARCH=amd64 go build -o hello main.go
</code></pre>

<p>Here is the <a href="https://golang.org/doc/install/source#environment" rel="nofollow noreferrer">list of all the supported operating system</a> with which you can easily do cross compilation using Go lang. </p>

CC BY-SA 3.0
2017-08-13T20:18:47.533Z
2017-08-13T20:18:47.533Z
mohan08p
<p>When <code>go build</code> is issued on a Windows host a .exe binary is created. How to ensure that a Linux binary is created on a Windows host?</p>

<p>The following was tried, but did not solve the issue:</p>

<pre><code>GOOS=linux;go build hello-world.go
</code></pre>

CC BY-SA 3.0
2017-08-07T05:30:10.733Z
replace the semicolon with space
akhy
<docker><linux><windows>
How to create Golang Linux binaries using a Windows host
030
# How to create Golang Linux binaries using a Windows host

(Asked by: username_0 on 2017-08-06)

When go build is issued on a Windows host a .exe binary is created. How to ensure that a Linux binary is created on a Windows host?
The following was tried, but did not solve the issue:
GOOS=linux;go build hello-world.go

**Tags:** docker, linux, windows

> **Comments:**
> **username_1**: replace the semicolon with space  

---

## 3 answers

### Answer by username_3 on 2017-08-13. Score: 4 | Accepted answer

You can easily set the target operating system and processor architecture using the environment variables GOOS and GOARCH respectively. So, as you want to build it for linux operating system, following command with above environment variables will do, 
$ GOOS=linux GOARCH=amd64 go build -o hello main.go

Here is the list of all the supported operating system with which you can easily do cross compilation using Go lang.


---

### Answer by username_0 on 2017-08-06. Score: 3

It seems that docker build opens a new shell and export is needed. The following command created a Linux binary:
export GOOS=linux; go build hello-world.go


---

### Answer by username_2 on 2020-02-21. Score: 0

Windows also has a one-liner solution, just a bit uglier than the linux version. Any of the below lines will do the job:
cmd /C "SET GOOS=linux&& SET GOARCH=amd64&& go build main.go"
cmd /C "SET "GOOS=linux" && SET "GOARCH=amd64" && go build main.go"

Be careful, if you just write ...SET GOOS=linux && SET ... then you will get an error message 
cmd/go: unsupported GOOS/GOARCH pair linux /amd64

because there will be an extra space after the value linux. In order to avoid this, use either the condensed form linux&& or the other version with quotation marks.


---

