Packaging Go Projects on Linux Operating System

Click the blue text above ● Follow Jie Chuang Yuan Linux

When packaging a project in Go, it is a common requirement to package dependencies together to ensure that it runs without errors due to missing dependencies on other machines. Here are some methods to achieve this:

Method 1: Using Go Modules

Starting from Go 1.11, Go introduced modules (Go Modules) to manage dependencies. Using Go Modules ensures that all dependencies are correctly packaged and deployed.

1. Initialize the module Run the following command in the project root directory to initialize the module:

go mod init <module_name>

For example:

go mod init gitee.com/datutu2015/go-do-excel

The following command can tidy up dependencies, add new dependencies, or remove unnecessary ones.

go mod tidy

2. Add dependencies Import the required packages in the code, and Go will automatically download and manage these dependencies. For example:

import (
    "fmt"
    "strconv"
    "github.com/xuri/excelize/v2"
)

3. Compile the project Run the following command in the project root directory to compile the project:

go build -o myapp main.go

This will generate an executable file <span>myapp</span>, which contains all statically linked dependencies and can be run directly on systems without the dependencies, as long as the operating system and architecture match the target at compile time.

4. Package Package the generated executable file into a compressed file:

tar -czvf myapp.tar.gz myapp

Method 2: Using Static Linking

Static linking can embed all dependency libraries directly into the executable file, making the generated executable independent and runnable on systems without these libraries installed.

1. Compile the project Use the following command to compile the project, ensuring static linking:

1). Specify target operating system linux and amd64 architecture

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o myapp main.go
  • <span>CGO_ENABLED=0</span>: Disables CGO to ensure the generated executable is statically linked.

  • <span>GOOS=linux</span> and <span>GOARCH=amd64</span>: Specify the target operating system linux and amd64 architecture.

2). Specify target operating system linux and arm64 architecture

  • CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o myapp main.go
    
  • <span>CGO_ENABLED=0</span>: Disables CGO to ensure the generated executable is statically linked.

  • <span>GOOS=linux</span> and <span>GOARCH=arm64</span>: Specify the target operating system linux and arm64 architecture.

In 1) and 2), the generated Linux executable file can be packaged into a compressed file:

tar -czvf myapp.tar.gz myapp

3). Specify target operating system windows and amd64 architecture

CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o your_app_name.exe -ldflags="-w -s"  main.go

To ensure the generated executable is statically linked, you can add the <span>-ldflags="-w -s"</span> parameter during compilation, which removes debugging information and reduces the size of the generated file.

Verify the generated file

The generated <span>.exe</span> file can run on Windows systems. If you need to verify this file on a Linux system, you can use the <span>file</span> command to check the file type:

file your_app_name.exe

The output should include information like “executable, statically linked”.

Notes

  • If the project includes C language extensions (via <span>cgo</span>), additional configuration of the cross-compilation toolchain may be required.

  • Ensure that the code in the project does not depend on Linux-specific features or resources, otherwise issues may arise when running on Windows.

Summary

  • Use Go Modules: Suitable for most modern Go projects, simple and easy to manage. It can run directly on systems without installed dependencies, as long as the operating system and architecture match the target at compile time.
  • Static linking is recommended: Generates independent executable files suitable for running on systems without installed dependencies, specifying the target operating system and architecture.

Choose the appropriate method to package dependencies based on your specific needs.

If this article has been helpful to you, please follow the WeChat public account “Jie Chuang Yuan Linux” for more technical content!

Leave a Comment