Golang (Go)
Go is an open-source programming language developed by Google, known for its simplicity, efficiency, and strong support for concurrent programming.
Installation on Linuxβ
This guide covers the installation of Go on a Linux-based system.
-
Download the latest Go package from the official downloads page.
curl -OL https://go.dev/dl/go1.22.4.linux-amd64.tar.gzwarningEnsure you download the correct package for your system architecture.
-
Extract the archive into
/usr/local, removing any previous Go installation.sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.22.4.linux-amd64.tar.gz -
Add the Go binary directory to your system's
PATH. Add the following line to your~/.profileor~/.zshrcfile:export PATH=$PATH:/usr/local/go/bin -
Apply the changes to your current session:
source ~/.profile -
Verify the installation by checking the Go version:
go versionThe output should display the installed Go version, for example:
go version go1.22.4 linux/amd64.
"Hello, World!" Exampleβ
Follow these steps to create and run a simple "Hello, World!" program.
-
Create a new directory for your project:
mkdir go-example
cd go-example -
Initialize a new Go module:
go mod init example/hello -
Create a file named
hello.gowith the following content:package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
} -
Run the program:
go run .The output will be:
Hello, World!.
Using External Packagesβ
Go modules allow you to manage dependencies and use third-party packages from sources like pkg.go.dev.
-
Import an external package in your code. For this example, we use
rsc.io/quote:package main
import (
"fmt"
"rsc.io/quote"
)
func main() {
fmt.Println(quote.Go())
} -
Run
go mod tidyto add the new dependency to yourgo.modfile and download it:go mod tidy -
Execute your code to see the output from the external package:
go run .
Project Structureβ
A well-organized project structure is crucial for maintainability. Here are some recommended resources for structuring Go projects:
- Organizing a Go module (Official Go documentation)
- golang-standards/project-layout (Community-driven standard)