Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/clevergo/jsend
:100: JSend's implementation writen in Go(golang)
https://github.com/clevergo/jsend
go go-api go-jsend jsend rest rest-api
Last synced: about 1 month ago
JSON representation
:100: JSend's implementation writen in Go(golang)
- Host: GitHub
- URL: https://github.com/clevergo/jsend
- Owner: clevergo
- License: mit
- Created: 2020-01-14T04:41:36.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-29T03:46:18.000Z (over 3 years ago)
- Last Synced: 2024-06-19T20:51:34.464Z (6 months ago)
- Topics: go, go-api, go-jsend, jsend, rest, rest-api
- Language: Go
- Homepage: https://clevergo.tech
- Size: 42 KB
- Stars: 22
- Watchers: 5
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-extra - jsend - 01-14T04:41:36Z|2021-06-29T03:46:18Z| (Utilities / Fail injection)
README
# JSend's implementation writen in Go(golang)
[![Build Status](https://img.shields.io/travis/clevergo/jsend?style=flat-square)](https://travis-ci.org/clevergo/jsend)
[![Coverage Status](https://img.shields.io/coveralls/github/clevergo/jsend?style=flat-square)](https://coveralls.io/github/clevergo/jsend?branch=master)
[![Go.Dev reference](https://img.shields.io/badge/go.dev-reference-blue?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/clevergo.tech/jsend?tab=doc)
[![Go Report Card](https://goreportcard.com/badge/github.com/clevergo/jsend?style=flat-square)](https://goreportcard.com/report/github.com/clevergo/jsend)
[![Release](https://img.shields.io/github/release/clevergo/jsend.svg?style=flat-square)](https://github.com/clevergo/jsend/releases)
[![Downloads](https://img.shields.io/endpoint?url=https://pkg.clevergo.tech/api/badges/downloads/total/clevergo.tech/jsend&style=flat-square)](https://pkg.clevergo.tech/clevergo.tech/jsend)
[![Chat](https://img.shields.io/badge/chat-telegram-blue?style=flat-square)](https://t.me/clevergotech)
[![Community](https://img.shields.io/badge/community-forum-blue?style=flat-square&color=orange)](https://forum.clevergo.tech)This package is an implementation of [JSend](https://github.com/omniti-labs/jsend) specification written in Go(golang).
## Installation
```shell
go get clevergo.tech/jsend
```## Usage
Usage is pretty simple.
```go
// 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:
```go
// 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](https://github.com/clevergo/examples/tree/master/jsend) 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.```go
jsend.Success(w, data, http.StatusOK)
jsend.Fail(w, data, http.StatusForbidden)
jsend.Error(w, message, http.StatusInternalServerError)
```