Code Explainers

Code explainers tagged #http-handlers

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