Code Explainers

Code explainers tagged #http-handlers

go
package signup
 
var wizardCodec = securecookie.New(
	[]byte(os.Getenv("WIZARD_HASH_KEY")),

A cookie-backed signup wizard in Gin

cookies encryption multi-step-form
Intermediate 9 steps
go
package handlers
 
import (
	"net/http"

Locale-aware pricing in a Gin handler

internationalization content-negotiation currency-formatting
Intermediate 9 steps
go
package api
 
import (
	"errors"

Turning Gin validation errors into JSON

validation error-handling http-handlers
Intermediate 9 steps
go
package events
 
import (
	"net/http"

Two-pass JSON dispatch in Gin

polymorphic-json request-binding validation
Intermediate 8 steps
go
package handlers
 
type ListFilters struct {
	Status    string `form:"status" binding:"omitempty,oneof=active archived all"`

Cross-field query validation in Gin

validation struct-tags query-binding
Intermediate 9 steps
go
package handlers
 
import (
	"net/http"

Sparse fieldsets in a Gin handler

sparse-fieldsets query-parsing json-serialization
Intermediate 9 steps
go
package handlers
 
import (
	"encoding/json"

Parsing search query params in a Go handler

http-handlers input-validation defaults
Intermediate 7 steps
go
package handler
 
type LineItem struct {
	SKU      string  `json:"sku" binding:"required,alphanum"`

Structured validation errors in Gin

validation json-binding error-handling
Intermediate 8 steps
go
func ListArticles(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
		if err != nil || page < 1 {

Building a paginated list endpoint in Gin

pagination query-parameters input-validation
Intermediate 8 steps
go
package handlers
 
import (
	"net/http"

Validating query params with Gin binding tags

validation struct-tags pagination
Intermediate 8 steps
go
package handlers
 
import (
	"fmt"

Handling secure file uploads in Gin

file-upload validation security
Intermediate 7 steps
go
package server
 
import (
	"context"

Composing HTTP middleware in Go

middleware http-handlers closures
Intermediate 8 steps
go
func (h *TransactionHandler) ExportCSV(c *gin.Context) {
	ctx := c.Request.Context()
	filters := parseTransactionFilters(c)
 

Streaming a CSV export in Gin

streaming csv-export database-cursor
Intermediate 8 steps
go
package handlers
 
import (
	"net/http"

Rendering HTML pages with Gin handlers

http-handlers templating error-handling
Beginner 8 steps
go
package handlers
 
import (
	"net/http"

Reading path parameters in Gin

routing path-parameters wildcards
Beginner 7 steps