SoFunction
Updated on 2025-03-02

How to package GO language into .exe programs

The power of Go is that it can easily compile source code into a standalone executable file. The following are the detailed steps to package Go programs into .exe files under Windows system.

1. Install the Go compiler

Before packaging, make sure that the Go compiler is installed on your system. If not installed, you can follow the steps below to install:

Visit Go's official website and download the Go language installation package suitable for your operating system.

After the installation is complete, enter the following command in the command line to verify that Go is installed correctly:

go version

You should see an output similar to the following, indicating that the installation is successful:

go version go1.20.3 windows/amd64

2. Write Go Programs

First, write a simple Go program. Create a file named as follows:

package main
import "fmt"
func main() {
    ("Hello, Go!")
}

This program will output "Hello, Go!" when it is run.

3. Set environment variables (optional)

If you want to generate executables for different platforms (such as Linux or macOS) on Windows, you can set the environment variables GOOS and GOARCH.

GOOS specifies the target operating system (such as windows, linux, darwin, etc.).

GOARCH specifies the target architecture (such as amd64, 386, etc.).

If you are developing on Windows and want to generate .exe files under Windows, you can omit this step. If you need to compile executable files from other platforms, you can use the following command:

set GOOS=windows

set GOARCH=amd64

4. Compile Go program as EXE file

Open the terminal or command line, navigate to the directory where your Go source file is located, and run the following command:

go build -o  

The -o option specifies the name of the output file, where the executable file we generate is.

It is the Go source file we want to compile.

After executing this command, the Go compiler will generate an executable file named.

5. Verify the generated EXE file

After compilation is complete, you can find the generated .exe file in the folder. Double-click to run the file, or run it on the command line:

The output should be:

Hello, Go!

This indicates that your Go program has been successfully packaged as a Windows executable.

6. Cross-compile (optional)

Go supports cross-compilation, that is, compile executable files suitable for other platforms on one platform. For example, you can compile binary files for Linux on Windows, just set the environment variables:

Compile 64-bit executable files under Linux:

set GOOS=linux
set GOARCH=amd64
go build -o myprogram-linux 

Compile the 64-bit executable file under macOS:

set GOOS=darwin
set GOARCH=amd64
go build -o myprogram-mac 

In this way, Go programs can be easily compiled across platforms without modifying code.

summary

Packaging Go programs into .exe files under Windows is very simple. You can quickly generate executable files through the go build command. With its concise syntax and powerful concurrency characteristics, Go language is suitable for developing high-performance, cross-platform applications. Through the above steps, you can easily compile Go applications into independent executables and deploy and run them on different platforms.

This is the end of this article about how to package GO into .exe programs. For more related contents of GO into .exe programs, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!