go-common

module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2026 License: GPL-3.0

README

go-common

A simple little go library of utils I use in just about every go project from small to large.

CI Go Report Card Go Reference

A collection of reusable Go utilities for HTTP APIs, logging, and validation.

Installation

go get github.com/wispberry-tech/go-common

Usage

import "github.com/wispberry-tech/go-common/pkg/common"
HTTP Utilities
// Query parameter parsing
page := common.ParseQueryInt(r, "page", 1)
active := common.ParseQueryBool(r, "active", false)
timestamp := common.ParseQueryTime(r, "created_at")
name := common.ParseQueryStringPtr(r, "name") // returns *string

// JSON responses
common.WriteJSONResponse(w, http.StatusOK, data)
common.WriteJSONError(w, http.StatusBadRequest, "INVALID_INPUT", "Invalid request", nil)

// Request body parsing
var req MyRequest
if err := common.ReadJSONBody(r, &req); err != nil {
    common.WriteJSONError(w, http.StatusBadRequest, "INVALID_JSON", "Invalid JSON body", nil)
    return
}
Logging
// Initialize at startup
common.InitializeLogger()
common.SetLogLevel("debug") // debug, info, warn, error

// Log messages
common.LogInfo("User logged in", "user_id", 123)
common.LogError("Database error", "error", err)
common.LogWarn("Rate limit approaching", "requests", 950)

// Formatted logging
common.LogInfof("Processing %d items", count)

// Context-aware logging
ctx := common.WithContext(context.Background(), logger)
logger := common.FromContext(ctx)
Validation
type CreateUserRequest struct {
    Email    string `json:"email" validate:"required,email"`
    Name     string `json:"name" validate:"required,min=2,max=100"`
    Password string `json:"password" validate:"required,min=8"`
}

if err := common.Validate.Struct(req); err != nil {
    response := common.FormatValidationErrors(err)
    common.WriteJSONError(w, http.StatusBadRequest, "VALIDATION_ERROR", response.Error, response.Details)
    return
}

Available Utilities

Utility Description
HTTP Helpers Query parsing, JSON request/response handling
Logging Structured, colorized logging with charmbracelet/log
Validation Request validation with go-playground/validator

Dependencies

License

GPL-3.0

Directories

Path Synopsis
Package common provides reusable utilities for building HTTP APIs in Go.
Package common provides reusable utilities for building HTTP APIs in Go.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL