Cross-Compiling Go Applications for Different OS

Tutorial 2 of 5

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.