Go (Golang) / Deploying Go Applications

Cross-Compiling Go Applications for Different OS

This tutorial covers how to cross-compile Go applications for different operating systems. You'll learn how to build your Go applications to run on various platforms from a single…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers best practices for compiling, packaging, and deploying Go applications.

Cross-Compiling Go Applications for Different OS

1. Introduction

This tutorial aims to guide you through the steps involved in cross-compiling Go applications for different operating systems. By the end of this tutorial, you will be able to build Go applications that can run on various platforms from one single codebase.

What you'll learn

  • The concept of cross-compilation
  • How to cross-compile Go applications for different operating systems

Prerequisites

  • Basic knowledge of Go programming
  • Go installed on your system

2. Step-by-Step Guide

Cross-compiling in Go is made possible by the GOOS and GOARCH environment variables. These variables allow you to specify the target operating system (OS) and architecture respectively.

  • GOOS: This variable represents the target operating system. For example, it could be windows, darwin (for MacOS), linux, etc.
  • GOARCH: This variable represents the target architecture, such as amd64, 386, arm, etc.

Example

Let's say we have a simple Go program that we want to compile for different OS. The code is as follows:

package main

import (
    "fmt"
    "runtime"
)

func main() {
    fmt.Printf("OS: %s, Arch: %s\n", runtime.GOOS, runtime.GOARCH)
}

This program prints the OS and architecture it was built for when it is run.

To build this for a Windows 64-bit system, we would set the GOOS to "windows" and the GOARCH to "amd64". The command would look like this:

GOOS=windows GOARCH=amd64 go build main.go

This will output a file named main.exe, which can be run on a Windows 64-bit system.

3. Code Examples

Example 1: Linux 32-bit system

To compile the same program for a Linux 32-bit system, we would set the GOOS to "linux" and the GOARCH to "386". The command would look like this:

GOOS=linux GOARCH=386 go build main.go

This will output a file named main, which can be run on a Linux 32-bit system.

Example 2: MacOS 64-bit system

For a MacOS 64-bit system, we would set the GOOS to "darwin" and the GOARCH to "amd64". The command would look like this:

GOOS=darwin GOARCH=amd64 go build main.go

This will output a file named main, which can be run on a MacOS 64-bit system.

4. Summary

In this tutorial, we have learned about cross-compiling Go applications for different operating systems. We learned how to use the GOOS and GOARCH environment variables to specify the target OS and architecture.

For further learning, you can explore more about the different values available for GOOS and GOARCH, and experiment with cross-compiling your Go programs for those different platforms.

You can also look into how to automate this process using build scripts or continuous integration tools.

5. Practice Exercises

Here are some exercises for you to practice:

  1. Write a Go program that prints "Hello, world!" and cross-compile it for a Windows 32-bit system.
  2. Write a Go program that calculates the factorial of a number and cross-compile it for a Linux ARM system.
  3. Write a Go program that accepts user input and cross-compile it for a MacOS 64-bit system.

Solutions

  1. Hello, world program:
package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}

Compile for Windows 32-bit: GOOS=windows GOARCH=386 go build main.go

  1. Factorial program:
package main

import (
    "fmt"
    "os"
    "strconv"
)

func factorial(n int) int {
    if n == 0 {
        return 1
    }
    return n * factorial(n-1)
}

func main() {
    arg := os.Args[1]
    n, _ := strconv.Atoi(arg)
    fmt.Printf("Factorial of %d is %d\n", n, factorial(n))
}

Compile for Linux ARM: GOOS=linux GOARCH=arm go build main.go

  1. User input program:
package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter text: ")
    text, _ := reader.ReadString('\n')
    fmt.Println("You entered:", text)
}

Compile for MacOS 64-bit: GOOS=darwin GOARCH=amd64 go build main.go

With these exercises, you'll get hands-on experience with cross-compiling Go applications for different operating systems.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Age Calculator

Calculate age from date of birth.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Backlink Checker

Analyze and validate backlinks.

Use tool

Favicon Generator

Create favicons from images.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help