Go Development and Execution Environment

From NovaOrdis Knowledge Base
Revision as of 21:46, 5 August 2024 by Ovidiu (talk | contribs) (→‎Building a Linux Image on Mac)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Internal

Overview

The go Tool

go is a tool used to manage source code. More details:

go

Environment Variables

Go Environment Variables

Compilation

go build

Execution

go run

Building a Linux Image on Mac

Once can do cross-compilation or build the executable in a local Linux container. This section describes how to build the executable from a local Linux container.

First, identify or build a "builder" image that contains the Go build tools. Let's assume that such an image is example.com/go1.22-builder:latest.

If your project does not declare dependencies on local unpublished module code, it is sufficient to mount the project directory in the build container. However, if the project declares dependencies on local unpublished module code, then we need to mount the directory that contains both source trees. Assuming we're building the project /Users/ovidiu/src/github.com/orgA/projectA and projectA declares a local dependency on ../../orgB/projectB, when we need to mount their common parent /Users/ovidiu/src/github.com:

docker run --rm -it --mount type=bind,src=/Users/ovidiu/src/github.com,target=/src example.com/go1.22-builder:latest
cd /src/orgA/projectA
./build

The build process will place the executable somewhere inside the source directory, depending on how the build system is set up.

The executable will be accessible from Mac.

To verify it is indeed Linux executable format:

file ./myexec
ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, Go BuildID=..., with debug_info, not stripped

TODO

Deplete, merge and delete Go Concepts - Runtime