Go — Get Started
Installation
Download Go from go.dev/dl:
# Verify installation
go version
# go version go1.22.0 linux/amd64
# Check Go environment
go env
Creating a module
# Create a new directory
mkdir myproject
cd myproject
# Initialize a module
go mod init myproject
# This creates go.mod:
# module myproject
# go 1.22
Your first program
Create main.go:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Running the program
# Run directly
go run main.go
# Build a binary
go build -o myapp main.go
./myapp
Project structure
myproject/
├── main.go
├── go.mod
├── go.sum # Dependencies (auto-generated)
├── cmd/ # Executable entry points
│ └── server/
│ └── main.go
├── internal/ # Private packages
│ └── handler/
│ └── handler.go
├── pkg/ # Public packages
│ └── utils/
│ └── utils.go
└── README.md
Adding dependencies
# Add a dependency
go get github.com/gin-gonic/gin
# Update dependencies
go get -u ./...
# Tidy (remove unused)
go mod tidy
# Download all dependencies
go mod download
Running tests
// main_test.go
package main
import "testing"
func TestAdd(t *testing.T) {
result := add(2, 3)
if result != 5 {
t.Errorf("add(2, 3) = %d; want 5", result)
}
}
# Run tests
go test
# Run with verbose output
go test -v
# Run specific test
go test -run TestAdd
# Test coverage
go test -cover
Build for different platforms
# Linux
GOOS=linux GOARCH=amd64 go build -o myapp-linux
# macOS
GOOS=darwin GOARCH=arm64 go build -o myapp-mac
# Windows
GOOS=windows GOARCH=amd64 go build -o myapp.exe
Useful commands
| Command | Description |
|---|---|
go run | Run a program |
go build | Compile a binary |
go test | Run tests |
go fmt | Format code |
go vet | Static analysis |
go mod init | Initialize module |
go get | Add dependency |
go doc | Show documentation |
IDE setup
Recommended editors:
- VS Code with Go extension
- GoLand by JetBrains
- Vim with vim-go plugin
Mini Practice
- Initialize a Go module and create
main.go - Run the program with
go run - Build a binary with
go build - Add a test file and run
go test
Up Next
In the next lesson, you'll learn about Syntax — Go's basic syntax and conventions.
Related Topics
Frequently Asked Questions about Get Started
What is Get Started in Go?
Get Started is a fundamental concept in Go. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Get Started?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Get Started.
Why is Get Started important in Go?
Get Started is essential for Go development. Understanding this concept will help you write better code and solve real-world problems more effectively.