Skip to content

mrjosh/respond.go

Repository files navigation

Go Respond

This package is provided to be used on golang and it gives clean methods to handle json response with specific predetermined messages.

Install

$ go get github.com/mrjosh/respond.go

Usage

import package

import "github.com/mrjosh/respond.go"

create respond instance

// argument rw is http.ResponseWriter or 
// gin.Writer if youre using gin-gonic
var jspon = respond.NewWithWriter(rw)

// or if you want to use custom languages
var jspon = respond.NewWithWriter(rw).Language("fa")

Some are shown below:

When request succeeds and contains data to return as a result:

jspon.Succeed(map[string] interface{} {
  "some_key": "some_data"
})

When deletion action succeeds:

jspon.DeleteSucceeded()

When updating succeeds:

jspon.UpdateSucceeded()

When insertion succeeds:

jspon.InsertSucceeded()

When deletion action fails:

jspon.DeleteFaild()

When updating fails:

jspon.UpdateFaild()

when insertion fails:

jspon.InsertFaild()

Not Found Error:

jspon.NotFound()

When parameters entered are wrong:

jspon.WrongParameters()

When requested method is not allowed:

jspon.MethodNotAllowed()
jspon.RequestFieldNotFound()

Validation errors:

jspon.ValidationErrors(map[string] interface{} {
  "some_key": "some_validation_errors_data"
})

###customization You can do more:

jspon.SetStatusCode(http.StatusOK).setStatusText("Success.").RespondWithMessage("Your custom message")

License

The MIT License (MIT). Please see License File for more information.