A comprehensive repository for learning and experimenting with Go, set up and tested using:
- IDE: GoLand 2022.3.2
- Go Version: go1.19.5
Below are the initial setup steps to prepare your environment and start experimenting with Go modules. Ensure your terminal is pointed to the root folder of this project before proceeding.
Click to expand
Creating a module directory is your first step in organizing your Go project. Modules in Go serve as containers for packages, and having a dedicated directory for each module helps in managing dependencies, versioning, and package distribution efficiently. This step is crucial when you're starting a new project or adding a new module to an existing project to ensure that your codebase remains organized and scalable.
mkdir module_name
Click to expand
Initializing a Go module with go mod init
sets the foundation for managing your project's dependencies.
This command creates a go.mod
file, marking the current directory as the root of a module.
It enables Go to track and manage the versions of external packages your project depends on, facilitating reproducible builds and simplifying dependency management.
This step is essential at the beginning of project development or when adding new dependencies.
go mod init module_path
The go mod init
command creates a go.mod
file to track your code's dependencies.
Initially, this file will only include your module's name and the Go version your code supports.
As you add dependencies, go.mod
will list the versions your code depends on, ensuring reproducible builds and direct control over module versions.
Note: For published modules, the module path must be a downloadable path by Go tools, typically your code's repository URL, e.g.,
github.com/squidmin/go-labs/example
.
Click to expand
Running go mod tidy
ensures that the go.mod
and go.sum
files accurately reflect the dependencies actually used in your project.
This command adds missing dependencies, removes unused ones, and updates go.mod
and go.sum
to match the source code.
It's particularly useful after adding or removing imports in your code or when preparing to commit your code to version control, ensuring a clean, up-to-date record of dependencies.
go mod tidy
Use the go mod tidy
command to clean up your go.mod
and go.sum
files.
This step adds any missing module dependencies and removes unused ones, ensuring that your module dependencies are accurate and up-to-date.
More info: go mod tidy
Click to expand
Compiling your Go code into an executable with go build
and then installing it with go install
makes it easy to distribute and run your application.
This process is crucial for creating standalone applications that can be executed without the Go runtime.
It's particularly relevant when you're ready to deploy your application or share it with users who may not have Go installed.
-
Navigate to Your Project Directory: Ensure you're in the directory containing your
main
package.cd ./directory_name
-
Build Your Code (Optional): This step compiles your code and generates an executable in the current directory. It's useful for testing the build process or when you need an executable in a specific location.
go build
Note: You can also build code from outside the module by specifying a path:
go build ./directory_name
-
Install Your Application:
cd ./directory_name # Navigate to the module go install
-
Adding the Executable to Your
PATH
(Optional):After building or installing your Go application, you might want to make the executable globally accessible from the command line, regardless of your current working directory. This is particularly useful for tools or applications you plan to use frequently across various projects or directories.
To add the executable to your
PATH
, you can use theexport
command in Unix-like operating systems, including Linux and macOS. This command temporarily modifies thePATH
environment variable for the current terminal session. To make this change permanent, you'll need to add the export command to your shell's startup file, such as.bashrc
,.bash_profile
, or.zshrc
, depending on your shell and operating system.The following command uses
go list -f '{{.Target}}'
to dynamically find the installation path of your Go executable and adds it to yourPATH
:export PATH=$PATH:$(go list -f '{{.Target}}')
Note: This step assumes you've used
go install
to compile and install your executable.go install
places binaries in the$GOPATH/bin
directory (or$GOBIN
if set), which is the path resolved bygo list -f '{{.Target}}'
. If you're managing your Go environment correctly,$GOPATH/bin
should already be in yourPATH
. However, if you find it's not, or if you've compiled your executable withgo build
and placed it in a custom directory, this command can be adapted to include that directory in yourPATH
.Important: Remember, changes made with the
export
command to thePATH
are temporary and only affect the current terminal session. For a permanent solution, you need to add the command to your shell's startup file as mentioned earlier.
Click to expand
To run an installed executable with options:
./executable_name -o --option -k=val --key=value
Or if the executable has been added to your PATH
:
executable_name -o --option -k=val --key=value
Click to expand
Use the -tags
flag with go build
or go run
:
go build -tags filename .
e.g.,
go build -tags arrays ./05_arrays_and_slices/01_declaration_and_initialization/arrays
go run -tags filename .
e.g.,
go run -tags arrays ./05_arrays_and_slices/01_declaration_and_initialization/arrays
Click to expand
The ability to run a Go script using go run
without explicitly compiling it first is invaluable during development.
It allows for rapid testing and iteration, letting developers focus on writing and refining code without worrying about the build process.
This approach is ideal for development environments where quick feedback on code changes is crucial.
go run script_name.go
Understanding the context and purpose behind these actions can significantly enhance your learning experience and effectiveness in Go development.
For a more in-depth introduction to Go, consider the official "Getting Started" tutorial.