Learn Go

Interactive examples you can run right in your browser. Click "Try it" to experiment!

๐Ÿš€

Simple HTTP Server

Build a web server in just a few lines. Go's net/http package is production-ready out of the box.

net/http web server
package main

import (
    "net/http"
    "fmt"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, World!")
    })
    http.ListenAndServe(":8080", nil)
}
๐Ÿ“ฆ

JSON Encoding

Struct tags give you fine control over JSON field names. Perfect for building APIs.

encoding/json struct tags
package main

import (
    "encoding/json"
    "fmt"
)

type User struct {
    Name  string `json:"name"`
    Email string `json:"email"`
}

func main() {
    u := User{Name: "Alice", Email: "alice@example.com"}
    data, _ := json.Marshal(u)
    fmt.Println(string(data))
}
๐Ÿงช

Mock HTTP Client

Go's interfaces make mocking easy. Implement RoundTripper to fake HTTP responses.

testing mocking
package main

import (
    "io"
    "net/http"
    "strings"
)

type MockTransport struct{}

func (m *MockTransport) RoundTrip(*http.Request) (*http.Response, error) {
    return &http.Response{
        StatusCode: 200,
        Body: io.NopCloser(strings.NewReader("mocked!")),
    }, nil
}

func main() {
    client := &http.Client{Transport: &MockTransport{}}
    resp, _ := client.Get("https://example.com")
    body, _ := io.ReadAll(resp.Body)
    println(string(body))
}
๐Ÿ”—

Calling C from Go

CGO lets you call C code directly. Put C in a comment, import "C", and you're set.

cgo C interop
package main

/*
#include <stdio.h>
void greet() { printf("Hello from C!\n"); }
*/
import "C"

func main() {
    C.greet()
}
๐Ÿ›

Find the Bug!

This code panics. Can you find why? Use our debugger to step through it!

debugging panic
package main

func check(s string) bool {
    return s[:3] == "foo"  // ๐Ÿ’ฅ What if s is short?
}

func main() {
    check("foobar")  // OK
    check("hi")      // Panic!
}

๐Ÿ“š The Go Programming Language

Examples from the book by Alan A. A. Donovan and Brian W. Kernighan

Chapter 1: Tutorial

Getting started with Go - Hello World, command-line, loops

Chapter 2: Program Structure

Names, declarations, variables, types, scope

Chapter 3: Basic Data Types

Integers, floats, strings, constants

Chapter 4: Composite Types

Arrays, slices, maps, structs, JSON

Chapter 5: Functions

Variadic functions, defer, closures, recursion

Chapter 6: Methods

Methods, receivers, embedding

Chapter 7: Interfaces

Interface types, satisfaction, values

Chapter 8: Goroutines & Channels

Concurrency, channels, select, cancellation

Chapter 9: Shared Variables

Mutexes, sync.Once, race conditions

Chapter 11: Testing

Unit tests, table-driven tests, benchmarks

Chapter 12: Reflection

reflect package, type introspection

Chapter 13: Low-Level

unsafe package, memory layout