1 Introduction to Go

Go is an open-source language by Google. It is statically typed, compiled, and designed for simplicity and performance.

Why? It handles concurrency (multiple tasks at once) brilliantly with Goroutines, making it perfect for cloud servers and microservices.

Download from go.dev. Once installed, verifying it by running go version in your terminal. You'll also need to set up your GOPATH, though newer versions handle this automatically with Modules.

package main
import "fmt"

func main() {
    fmt.Println("Hello, Go!")
}

Run it with go run main.go.

2 Go Basics

Explicit: var x int = 10
Short (Inference): x := 10 (Only inside functions)
Types: string, int, float64, bool.

Values that cannot change. Defined using const.

const Pi = 3.14
const AppName = "My App"

Use the fmt package.
fmt.Println("Hi"): Prints a line.
fmt.Printf("Age: %d", age): Prints formatted string (%d for int, %s for string).

3 Control Flow

No parentheses required around the condition.

if x > 10 {
    fmt.Println("Big")
} else {
    fmt.Println("Small")
}

Go only has for. There is no while.

// Standard loop
for i := 0; i < 5; i++ { }

// Like a while loop
for x < 10 { }

4 Functions in Go

Declared with func. Types come after names.

func add(x int, y int) int {
    return x + y
}

A unique feature of Go. Functions can return more than one result.

func swap(x, y string) (string, string) {
    return y, x
}
a, b := swap("hello", "world")

5 Go Data Structures

Arrays: Fixed size. [5]int.
Slices: Dynamic size. Wrappers around arrays. This is what you use 99% of the time. []int{1, 2, 3}.

Key-value pairs (like Python dicts).

m := make(map[string]int)
m["Age"] = 30

Custom data types holding multiple fields.

type Person struct {
    Name string
    Age  int
}

6 Pointers & Packages

Go gives you control over memory access without the complexity of C.

& gets the memory address. * gets the value at an address.

Packages: Code is organized in packages. package main is the entry point.
Modules: Dependency management (like package.json). Initialize with go mod init myapp.

Concurrency (Go's Superpower)

A lightweight thread managed by the Go runtime. Just add go before a function call to run it in the background.

go doSomething() // Runs concurrently

Pipes that connect concurrent goroutines. You can send values into channels from one goroutine and receive those values into another.

🛠 Practice Projects

Build a command-line tool that takes two numbers and an operator (+, -, *, /) as arguments and prints the result. Use os.Args to read input.

Write a program that reads a text file and counts the number of words in it. You'll learn about the os and bufio packages.

Create a program that fetches data from 3 different URLs simultaneously using goroutines and prints which one finished first. Teaches practical concurrency.

Say Hi! 👋

Have a question or just want to connect? Drop me a message!

📧 Sending to: samarth@skilledca.in
🎉

Message Sent!

Thanks for reaching out! I'll get back to you soon.