Getting Started with Go (Golang)
Go, often referred to as Golang, is an open-source programming language developed by Google engineers. It combines the simplicity of syntax similar to Python with the performance and safety of languages like C++. Go is designed for building fast, reliable, and scalable software, making it ideal for modern applications like cloud services, APIs, and DevOps tools.
In this tutorial, you’ll learn how to set up Go, write your first program, and explore core concepts.
Step 1: Install Go
Download and Install
- Visit the official Go downloads page: https://go.dev/dl/.
- Download the installer for your OS (Windows, macOS, or Linux).
Windows/macOS
- Run the downloaded
.msi(Windows) or.pkg(macOS) file and follow the prompts.
Linux
- Extract the tarball to
/usr/local:1
sudo tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz
- Add Go to your
PATHby adding this line to~/.bashrcor~/.zshrc:1
export PATH=$PATH:/usr/local/go/bin
- Run
source ~/.bashrc(or restart your terminal).
Verify Installation
Open a terminal and run:
1
go version
You should see output like go version go1.21.0 linux/amd64.
Step 2: Set Up Your Workspace
Go uses modules for dependency management. Create a project directory anywhere on your system (no need for a strict GOPATH structure in newer Go versions).
- Create a project folder:
1
mkdir my-go-project && cd my-go-project
- Initialize a Go module:
1
go mod init example.com/myproject
Step 3: Write Your First Program
Create a file hello.go with the following code:
1
2
3
4
5
6
7
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Explanation
package main: Declares this file as part of themainpackage (required for executables).import "fmt": Imports thefmtpackage for input/output operations.func main(): The entry point of the program.
Step 4: Run the Program
In the terminal, run:
1
go run hello.go
Output:
1
Hello, World!
Step 5: Learn Basic Syntax
Variables
Use var or shorthand := for type inference:
1
2
var name string = "Alice"
age := 30 // Type inferred as int
Functions
Define a function with parameters and return types:
1
2
3
func add(a int, b int) int {
return a + b
}
Control Structures
If-Else
1
2
3
4
5
if x > 10 {
fmt.Println("Large")
} else {
fmt.Println("Small")
}
For Loop
Go only has a for loop (no while keyword):
1
2
3
for i := 0; i < 5; i++ {
fmt.Println(i)
}
Step 6: Concurrency with Goroutines and Channels
Go’s standout feature is built-in concurrency using goroutines (lightweight threads) and channels (for communication).
Goroutine Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package main
import (
"fmt"
"time"
)
func printNumbers() {
for i := 1; i <= 3; i++ {
time.Sleep(1 * time.Second)
fmt.Println(i)
}
}
func main() {
go printNumbers() // Runs concurrently
fmt.Println("Started goroutine")
time.Sleep(4 * time.Second) // Wait for goroutine to finish
}
Channels Example
1
2
3
4
5
6
7
8
9
10
func main() {
ch := make(chan string)
go func() {
ch <- "Message from goroutine"
}()
msg := <-ch
fmt.Println(msg) // Output: Message from goroutine
}
Step 7: Dependency Management
Go Modules handle dependencies. To add a third-party package:
- Import it in your code:
1
import "github.com/gorilla/mux"
- Run
go mod tidyto download and install the package.
Next Steps
- Explore the standard library: https://pkg.go.dev/std.
- Build a REST API using
net/httpor frameworks like Gin. - Practice concurrency patterns (e.g., worker pools).
- Read “The Go Programming Language” book or the official Tour of Go.
Conclusion
You’ve installed Go, written a basic program, and explored its core features. Go’s simplicity and performance make it a powerful tool for modern development. Happy coding! 🚀