Go Development and Execution Environment: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(11 intermediate revisions by the same user not shown)
Line 6: Line 6:


=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>
 
=Compilation=
{{Internal|Go_Tool#build|<tt>go build</tt>}}
 
=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>:


To display the value, as seen by the Go runtime, execute:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
go env GOPATH
docker run --rm -it --mount type=bind,src=/Users/ovidiu/src/github.com,target=/src example.com/go1.22-builder:latest
</syntaxhighlight>
</syntaxhighlight>


==<tt>GOROOT</tt>==
<syntaxhighlight lang='bash'>
<font color=darkkhaki>Used to search for packages during compilation, when packages are being [[Go_Language_Modularization#Importing_Packages|imported]].</font>
cd /src/orgA/projectA
./build
</syntaxhighlight>


=Compilation=
The build process will place the executable somewhere inside the source directory, depending on how the build system is set up.
{{Internal|Go_Tool#build|<tt>go build</tt>}}
 
The executable will be accessible from Mac.


=Execution=
To verify it is indeed Linux executable format:
{{Internal|Go_Tool#run|<tt>go run</tt>}}
<syntaxhighlight lang='bash'>
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
</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