shelf

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 6 Imported by: 0

README

shelf

shelf is a small Go package that provides helper functions to simplify common HTTP request and response handling tasks.

Installation

go get github.com/primekobie/shelf

Features

  • JSON helper functions

Example

package main

import (
    "net/http"
    "github.com/primekobie/shelf"
)

type User struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}

func userHandler(w http.ResponseWriter, r *http.Request) {
    user := User{ID: 1, Name: "John Doe"}
    headers := http.Header{}
    headers.Set("X-Custom-Header", "value")

    err := shelf.WriteJSON(w, http.StatusOK, user, headers)
    if err != nil {
        // Handle error
    }
}

func updateHandler(w http.ResponseWriter, r *http.Request) {
    var payload struct {  Status string `json:"status"` }
    
    err := shelf.ReadJSON(r, &payload) // Read JSON
    if err != nil {
        // Handle error, for example:
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    // Process the payload...
    shelf.WriteJSON(w, http.StatusOK, map[string]string{"status": "updated"}, nil)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadJSON

func ReadJSON(r *http.Request, dest any) error

ReadJSON reads and decodes a JSON request body into the provided destination struct.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, payload any, headers http.Header) error

WriteJSON writes a JSON response to the http.ResponseWriter. It returns an error if the payload cannot be encoded to JSON.

Types

This section is empty.

Jump to

Keyboard shortcuts

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