Categorygithub.com/phonkee/go-response
modulepackage
0.4.2
Repository: https://github.com/phonkee/go-response.git
Documentation: pkg.go.dev

# README

response

Response is simple helper for rest api responses. The naming of api functions is: for setter Value and for getter GetValue (Only exception is String so we satisfy Stringer interface). Let's see some examples:

response.New(http.StatusOK).Write(w, r)

// or with default OK status

response.New().Write(w, r)
    

This writes following response to ResponseWriter (w).

{
  "status": 200,
  "message": "OK"
}

Response adds ability to add various data to response object. You can use method Data to set data or there are some shorthand for usual data keys.


response.New().Data("result", result).Write(w, r)

// or with a shorthand

response.New().Result(result).Write(w, r)

There is also shorthand to provide slice result that automatiaclly adds result_size.

response.New().SliceResult(result).Write(w, r)

Shortcuts

response serves following shortcuts to create responses

  • Data
  • Error
  • HTML
  • Result
  • SliceResult
  • Write

Example:

result := map[string]string{}
response.Result(result)

Is the same as doing

result := map[string]string{}
response.New(http.StatusOK).Result(result)

or even

result := map[string]string{}
response.New().Result(result)

Contributions

You are welcome to send PR.

# Functions

BadRequest returns response with StatusBadRequest */.
Body is helper to create response */.
CurrentFormat returns current key.
Data is helper to create status ok response.
Error is helper to create status ok response.
Format sets KeyFormat to given KeyFormat.
GetErrorStatus returns appropriate http status for given error.
HTML returns response set to HTML */.
New returns new response instance, if no status is given StatusOK is used */.
NotFound returns response with StatusNotFound */.
OK returns response with StatusOK */.
RegisterError registers error to given http status.
Result is helper to create status ok response.
SliceResult is helper to create status ok response.
Unauthorized returns response with StatusUnauthorized */.
Write writes to response and returns error */.

# Constants

No description provided by the author

# Variables

CamelCaseFormat sets common keys as camel case.
SnakeCaseFormat sets common keys as snake case.

# Structs

No description provided by the author

# Interfaces

Response interface provides several method for Response */.