Go Development and Execution Environment: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(17 intermediate revisions by the same user not shown)
Line 3: Line 3:
=Overview=
=Overview=
=The <tt>go</tt> Tool=
=The <tt>go</tt> Tool=
<code>go</code> is a tool used to manage source code. It has many commands.
<code>go</code> is a tool used to manage source code. More details: {{Internal|go_Tool#Overview|<tt>go</tt>}}


=Environment Variables=
=Environment Variables=
==<tt>GOPATH</tt>==
{{Internal|Go Environment Variables|Go Environment Variables}}
<FONT COLOR=DARKKHAKI>The <code>GOPATH</code> environment variable defines the [[Go_Language_Modularization#Workspaces|workspace]] directory. Go tools assume that your code is under the path designated by <code>GOPATH</code>. Used to search for packages during compilation, when packages are being [[Go_Language_Modularization#Importing_Packages|imported]].</font>
 
==<tt>GOROOT</tt>==
<font color=darkkhaki>Used to search for packages during compilation, when packages are being [[Go_Language_Modularization#Importing_Packages|imported]].</font>


=Compilation=
=Compilation=
{{Internal|Go_Tool#build|<tt>go build</tt>}}


=Execution=
=Execution=
{{Internal|Go_Tool#run|<tt>go run</tt>}}
=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 <code>example.com/go1.22-builder:latest</code>.
If your project does not declare dependencies on [[Go.mod#Using_Local_Module_Code|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 <code>/Users/ovidiu/src/github.com/orgA/projectA</code> and projectA declares a local dependency on <code>../../orgB/projectB</code>, when we need to mount their common parent <code>/Users/ovidiu/src/github.com</code>:
<syntaxhighlight lang='bash'>
docker run --rm -it --mount type=bind,src=/Users/ovidiu/src/github.com,target=/src example.com/go1.22-builder:latest
</syntaxhighlight>
<syntaxhighlight lang='bash'>
cd /src/orgA/projectA
./build
</syntaxhighlight>
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:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
cd $PROJECT_DIR
file ./myexec
go run ./src/main/main.go
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
</syntaxhighlight>
</syntaxhighlight>


=TODO=
=TODO=
<font color=darkkhaki>Deplete, merge and delete [[Go Concepts - Runtime]]</font>
<font color=darkkhaki>Deplete, merge and delete [[Go Concepts - Runtime]]</font>

Latest revision as of 21:46, 5 August 2024

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