Simple HTTP Server
Build a web server in just a few lines. Go's net/http package is production-ready out of the box.
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.
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.
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.
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!
package main func check(s string) bool { return s[:3] == "foo" // ๐ฅ What if s is short? } func main() { check("foobar") // OK check("hi") // Panic! }