Chapter 2. The Go compiler
2.1. Prerequisites Copy linkLink copied to clipboard!
2.2. Setting up a Go workspace Copy linkLink copied to clipboard!
Modern Go projects are built using modules. You can start with a single module and then optionally group multiple modules into a workspace to work on them simultaneously.
Procedure
Create a root directory for your projects, for example:
mkdir ~/go-projects/
$ mkdir ~/go-projects/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Change into the project directory:
cd ~/go-projects/
$ cd ~/go-projects/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Initialize a module:
Create a directory for your module:
mkdir <module_name>
$ mkdir <module_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Change into the module’s directory:
cd <module_name>
$ cd <module_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Initialize the module:
go mod init <module_name>
$ go mod init <module_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This command creates a single-module project.
If you want to create multiple modules, repeat this step for every module.
If you want to work on multiple modules at the same time, create a multi-module workspace:
Change into the project directory:
cd ~/go-projects/
$ cd ~/go-projects/
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Initialize a workspace to include multiple modules:
go work init <module_name_1> <module_name_n> ...
$ go work init <module_name_1> <module_name_n> ...
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.3. Compiling a Go program Copy linkLink copied to clipboard!
You can compile your Go program using the Go compiler. The Go compiler creates an executable binary file as a result of compiling.
Prerequisites
- A Go workspace with configured modules.
Procedure
Compile the Go sources in the current directory:
go build .
$ go build .
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.4. Running a Go program Copy linkLink copied to clipboard!
The Go compiler creates an executable binary file as a result of compiling. Complete the following steps to run your program.
Procedure
Use one of the following options to execute your Go program:
To run an compiled program, enter:
./<file_name>
$ ./<file_name>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Replace
<file_name>
with the name of your executable file.To compile the sources in the current directory and run the program in a single step, enter:
go run .
$ go run .
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.5. Installing compiled Go projects Copy linkLink copied to clipboard!
You can download and install third-party Go projects from online resources to use their executable files and libraries in further Go projects. After installation, the executable files and libraries of the project are copied according to the directories in the Go workspace. Its dependencies are installed as well.
Prerequisites
- A Go workspace with configured modules.
Procedure
Install a Go project:
go install <go_project>
$ go install <go_project>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.6. Downloading and installing Go projects Copy linkLink copied to clipboard!
You can download and install third-party Go projects from online resources to use their executable files and libraries in further Go projects. After installation, the executable files and libraries of the project are copied according to the directories in the Go workspace. Its dependencies are installed as well.
Prerequisites
- A Go workspace with configured modules.
Procedure
To download and install a Go project, enter:
go install <third_party_go_project>
$ go install <third_party_go_project>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: For information on possible values of third-party projects, enter:
go help importpath
$ go help importpath
Copy to Clipboard Copied! Toggle word wrap Toggle overflow