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:

  1. Create a new folder: mkdir hello-go
  2. Navigate to folder: cd hello-go
  3. Create main.go file
  4. Write the code below
  5. 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

๐Ÿง  Test Your Knowledge

Which command runs a Go program directly without building?