Go Tutorial #21: Input Validation and API Best Practices

In the previous tutorial, you learned how to read and write files in Go. Now let’s focus on making your APIs production-ready. Building an API that works is one thing. Building an API that is reliable, secure, and easy to use is another. In this tutorial, you will learn the best practices that separate hobby projects from production services. Input Validation with validator Manual validation gets messy fast. The go-playground/validator package lets you validate structs with tags: ...

April 13, 2026 · 8 min

Go Tutorial #20: File I/O — Reading and Writing Files

In the previous tutorial, you learned database access with sqlx. Now let us work with files. File I/O is one of the most common tasks in Go — reading configuration files, processing logs, exporting data, and more. Go makes file I/O simple with the os and bufio packages. And the io.Reader and io.Writer interfaces make everything composable. Reading an Entire File The simplest way to read a file is os.ReadFile: package main import ( "fmt" "log" "os" ) func main() { data, err := os.ReadFile("hello.txt") if err != nil { log.Fatal(err) } fmt.Println(string(data)) } os.ReadFile reads the entire file into memory as a byte slice. This is fine for small files. For large files, use a scanner or reader instead. ...

April 12, 2026 · 8 min

Go Tutorial #19: Database Access with sqlx

In the previous tutorial, you built JWT authentication for your API. But we stored users in memory — they disappear when the server restarts. It is time to use a real database. Go has a clean database interface in the standard library. And sqlx makes it even better by adding struct scanning and named queries. In this tutorial, you will learn how to use sqlx with PostgreSQL. database/sql — The Standard Interface Go’s database/sql package defines a standard interface for SQL databases. It works with any SQL database through drivers: ...

April 12, 2026 · 10 min

Go Tutorial #18: Middleware and JWT Authentication

In the previous tutorial, you learned how to test Go code. Now it is time to add authentication to your API. Middleware is the standard way to handle cross-cutting concerns like authentication, logging, and CORS. What is Middleware? Middleware is a function that runs before (or after) your handler. It sits between the request and the handler: Request → Middleware 1 → Middleware 2 → Handler → Response Common uses for middleware: ...

April 12, 2026 · 9 min

Go Tutorial #17: Testing in Go

In the previous tutorial, you built REST APIs with Gin. Now it is time to test your code. Go has a powerful testing framework built into the standard library — no external dependencies needed. Testing in Go is simple by design. Test files live next to the code they test, and you run them with one command: go test. Your First Test Create a file called math.go: package math func Add(a, b int) int { return a + b } func Divide(a, b int) (int, error) { if b == 0 { return 0, fmt.Errorf("cannot divide by zero") } return a / b, nil } Now create math_test.go in the same directory: ...

April 12, 2026 · 9 min

Go Tutorial #16: Building REST APIs with Gin

In the previous tutorial, you built HTTP servers with the standard net/http package. That works great for simple servers. But as your API grows, you need better routing, input validation, and middleware support. Gin is the most popular Go web framework with over 80,000 GitHub stars. It is fast, well-documented, and used in production by thousands of companies. In this tutorial, you will learn how to build REST APIs with Gin. ...

April 11, 2026 · 8 min

Go Tutorial #15: Building HTTP Servers with net/http

In the previous tutorial, you learned advanced error handling patterns. Now it is time to build something real — an HTTP server. Go has a powerful HTTP server in the standard library. No framework needed. The net/http package gives you everything for building web servers and REST APIs. Many production Go services use only the standard library. Your First HTTP Server package main import ( "fmt" "net/http" ) func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") } func main() { http.HandleFunc("/hello", helloHandler) fmt.Println("Server starting on :8080") err := http.ListenAndServe(":8080", nil) if err != nil { fmt.Println("Server error:", err) } } Run it: ...

April 11, 2026 · 8 min

Go Tutorial #14: Error Handling Patterns — Beyond if err != nil

In Go Tutorial #4, you learned the basics of error handling — the error interface and the if err != nil pattern. Now it is time to go deeper. Real applications need more than basic error checks. You need to know what went wrong, where it went wrong, and how to handle different errors differently. Go gives you tools for all of this. Error Wrapping When a function calls another function and gets an error, you should add context before returning it. Use fmt.Errorf with the %w verb to wrap errors: ...

April 11, 2026 · 10 min

Go Tutorial #13: Select, Context, and Concurrency Patterns

In the previous tutorial, you learned about channels. Now you will learn how to work with multiple channels at the same time using select, how to cancel operations with context, and some powerful concurrency patterns. These are the tools that make Go concurrency practical for real applications. The select Statement select lets you wait on multiple channel operations at the same time. It blocks until one of the channels is ready: ...

April 11, 2026 · 9 min

Go Tutorial #12: Channels — Communication Between Goroutines

In the previous tutorial, you learned about goroutines. But goroutines alone are not enough. They need a way to communicate. That is what channels are for. Channels are typed pipes that connect goroutines. One goroutine sends a value into the channel, another goroutine receives it. Channels make concurrent programming safe and simple. Go has a famous saying: “Do not communicate by sharing memory. Share memory by communicating.” Channels are how you do that. ...

April 10, 2026 · 9 min