Go Get Started
Install Go and create your first program
๐ Getting Started with Go
Ready to start coding in Go? This guide walks you through installing Go, setting up your development environment, and writing your first program. You'll be coding in minutes!
# Check if Go is installed
go version
# Expected output
go version go1.21.0 linux/amd64
Install Go
Windows
Download and run the installer
# Download from golang.org
# Run the .msi installer
# Restart command prompt
macOS
Use Homebrew or installer
# Using Homebrew
brew install go
# Or download .pkg file
Linux
Extract and set PATH
# Download tar.gz
sudo tar -C /usr/local -xzf go*.tar.gz
export PATH=$PATH:/usr/local/go/bin
Online
Try Go without installing
# Visit play.golang.org
# Write and run Go code
# Perfect for learning
๐น Verify Installation
Check if Go is properly installed:
# Check Go version
go version
# Check Go environment
go env GOPATH
go env GOROOT
Expected Output:
go version go1.21.0 darwin/amd64
/Users/username/go
/usr/local/go
๐น Create Your First Program
Let's create a simple "Hello World" program:
Steps:
-
Create a new folder:
mkdir hello-go -
Navigate to folder:
cd hello-go - Create main.go file
- Write the code below
- Run the program
// main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
fmt.Println("Welcome to Go programming!")
// Variables
name := "Go Developer"
fmt.Printf("You are now a %s!\n", name)
}
Output:
Hello, World!
Welcome to Go programming!
You are now a Go Developer!
๐น Run Your Program
There are two ways to run Go programs:
๐ธ Method 1: Direct Run
# Run directly (slower, good for development)
go run main.go
๐ธ Method 2: Build and Run
# Build executable (faster, good for production)
go build main.go
# Run the executable
./main # Linux/Mac
main.exe # Windows
๐น Go Workspace Setup
Organize your Go projects properly:
# Create a new Go module
mkdir my-go-project
cd my-go-project
go mod init my-go-project
# This creates go.mod file
# Now you can add dependencies
Project Structure:
my-go-project/
โโโ go.mod # Module definition
โโโ main.go # Main program
โโโ utils/ # Helper packages
โ โโโ helper.go
โโโ README.md # Documentation
๐น Essential Go Commands
Commands you'll use frequently:
go run
Run Go programs directly
go run main.go
go build
Compile to executable
go build -o myapp
go fmt
Format your code
go fmt ./...
go test
Run tests
go test ./...
๐น Practice Program
Try this interactive program to practice:
package main
import (
"fmt"
"time"
)
func main() {
// Get current time
now := time.Now()
hour := now.Hour()
// Greet based on time
var greeting string
if hour < 12 {
greeting = "Good morning"
} else if hour < 18 {
greeting = "Good afternoon"
} else {
greeting = "Good evening"
}
fmt.Printf("%s! Today is %s\n", greeting, now.Format("Monday, January 2, 2006"))
fmt.Println("You're learning Go programming!")
// Simple calculation
x, y := 15, 25
fmt.Printf("%d + %d = %d\n", x, y, x+y)
}
Output:
Good afternoon! Today is Monday, January 15, 2024
You're learning Go programming!
15 + 25 = 40