README

JSend's implementation writen in Go(golang)

Build Status Coverage Status Go.Dev reference Go Report Card Release Downloads Chat Community

This package is an implementation of JSend specification written in Go(golang).

Installation

go get clevergo.tech/jsend

Usage

Usage is pretty simple.

// success response
jsend.Success(w, data)
// fail response
jsend.Fail(w, data)
// error response
jsend.Error(w, message)
// error response with extra code
jsend.ErrorCode(w, message, code)
// error response with extra code and data
jsend.ErrorCodeData(w, message, code, data)

It can also be integrated with web framework, such as Gin, Echo, CleverGo:

// success response
ctx.JSON(http.StatusOK, jsend.New(data))
// fail response
ctx.JSON(http.StatusOK, jsend.NewFail(data))
// error response
ctx.JSON(http.StatusOK, jsend.NewError(message, code, data))

Checkout example for details.

Error Handling

It is application responsibility to handle error.

Status Code

By default status code http.StatusOK was used implicitly, it can also be specified by the last parameter if necessary.

jsend.Success(w, data, http.StatusOK)
jsend.Fail(w, data, http.StatusForbidden)
jsend.Error(w, message, http.StatusInternalServerError)

Documentation

Index

Constants

View Source
const (
	StatusError   = "error"
	StatusFail    = "fail"
	StatusSuccess = "success"
)

    Status constants

    Variables

    This section is empty.

    Functions

    func Error

    func Error(w http.ResponseWriter, message string, statuses ...int) error

      Error writes error body with the given message.

      func ErrorCode

      func ErrorCode(w http.ResponseWriter, message string, code int, statuses ...int) error

        ErrorCode writes error body with the given message and code.

        func ErrorCodeData

        func ErrorCodeData(w http.ResponseWriter, message string, code int, data interface{}, statuses ...int) error

          ErrorCodeData writes error body with the given message, code and data.

          func Fail

          func Fail(w http.ResponseWriter, data interface{}, statuses ...int) error

            Fail writes failed body with the given data.

            func Success

            func Success(w http.ResponseWriter, data interface{}, statuses ...int) error

              Success writes successful body with the given data.

              func Write

              func Write(w http.ResponseWriter, body Body, statuses ...int) error

                Write writes the body to http.ResponseWriter.

                If necessary, the status code can be specified through the third parameter.

                Types

                type Body

                type Body struct {
                	// The status indicates the execution result of request,
                	// it can be one of "success", "fail" and "error".
                	Status  string      `json:"status"`
                	Data    interface{} `json:"data,omitempty"`
                	Message string      `json:"message,omitempty"`
                	Code    int         `json:"code,omitempty"`
                }

                  Body contains

                  func New

                  func New(data interface{}) Body

                    New returns a success body with the given data.

                    func NewError

                    func NewError(message string, code int, data interface{}) Body

                      NewError returns a error body with given message.

                      func NewFail

                      func NewFail(data interface{}) Body

                        NewFail returns a fail body with the given data.

                        Source Files