go-response: github.com/phonkee/go-response Index | Files

package response

import "github.com/phonkee/go-response"

error helper package

Response helper interface for structured json responses. Response has also support to provide HTML content, but primarily it's

targeted to json rest apis

Response supports errors, raw content, etc..

Setter methods support chaining so writing response to http is doable on single line In next examples we use these variables

w http.ResponseWriter
r *http.Request

Example of responses

response.New(http.StatusInternalServerError).Error(errors.New("error")).Write(w, r)
response.New().Error(structError).Write(w, r)
response.New().Result(product).Write(w, r)
response.New().Result(products).ResultSize(size).Write(w, r)
response.New().SliceResult(products).Write(w, r)
response.New(http.StatusForbidden).Write(w, r)

Also there is non required argument status

body := map[string]string{
	"version": "1.0beta"
response.New(http.StatusOK).Body(body)Write(w, r)
response.New(http.StatusOK).Result(product).Write(w, r)
response.New(http.StatusOK).SliceResult(products).Write(w, r)

Minimal support for html responses

response.New().HTML("<html></html>").Write(w, r)


Package Files

api.go errors.go format.go response.go


const STATUS_HEADER = "X-Response-Status"


var (
    // CamelCaseFormat sets common keys as camel case
    CamelCaseFormat = format{
        ErrorKey:      "Error",
        MessageKey:    "Message",
        ResultKey:     "Result",
        ResultSizeKey: "ResultSize",
        StatusKey:     "Status",

    // SnakeCaseFormat sets common keys as snake case
    SnakeCaseFormat = format{
        ErrorKey:      "error",
        MessageKey:    "message",
        ResultKey:     "result",
        ResultSizeKey: "result_size",
        StatusKey:     "status",

func Format Uses

func Format(f format)

Format sets format to given format

func GetErrorStatus Uses

func GetErrorStatus(err error) int

GetErrorStatus returns appropriate http status for given error

func RegisterError Uses

func RegisterError(err error, status int)

RegisterError registers error to given http status

func Write Uses

func Write(w http.ResponseWriter, r *http.Request)

Write writes to response and returns error

type Response Uses

type Response interface {

    // set raw body
    Body(body interface{}) Response

    // Set Content type
    ContentType(contenttype string) Response

    // set data to response data
    Data(key string, value interface{}) Response

    // delete data value identified by key
    DeleteData(key string) Response

    // delete header by name
    DeleteHeader(name string) Response

    // sets `error` to response data
    Error(err interface{}) Response

    // returns response as []byte
    GetBytes() []byte

    // set header with value
    Header(name, value string) Response

    // set html and content type (for template rendering)
    HTML(html string) Response

    // set http message
    Message(message string) Response

    // custom json marshalling function
    MarshalJSON() (result []byte, err error)

    // set `result` on response data (shorthand for Data("result", data))
    Result(result interface{}) Response

    // set result_size on response
    ResultSize(size int) Response

    // set slice result (sets `result` and `result_size`)
    SliceResult(result interface{}) Response

    // set http status
    Status(status int) Response

    // return string value of response
    String() (body string)

    // Write complete response to writer
    Write(w http.ResponseWriter, request *http.Request)

Response interface provides several method for Response

func BadRequest Uses

func BadRequest() Response

BadRequest returns response with StatusBadRequest

func Body Uses

func Body(body interface{}) Response

Body is helper to create response

func Data Uses

func Data(key string, value interface{}) Response

Data is helper to create status ok response.

func Error Uses

func Error(err interface{}) Response

Error is helper to create status ok response.

func HTML Uses

func HTML(html string) Response

HTML returns response set to HTML

func New Uses

func New(statuses ...int) (result Response)

New returns new response instance, if no status is given StatusOK is used

func NotFound Uses

func NotFound() Response

NotFound returns response with StatusNotFound

func OK Uses

func OK() Response

OK returns response with StatusOK

func Result Uses

func Result(result interface{}) Response

Result is helper to create status ok response.

func SliceResult Uses

func SliceResult(result interface{}) Response

SliceResult is helper to create status ok response.

func Unauthorized Uses

func Unauthorized() Response

Unauthorized returns response with StatusUnauthorized

Package response imports 7 packages (graph) and is imported by 3 packages. Updated 2020-07-21. Refresh now. Tools for package owners.