Documentation
¶
Overview ¶
Package result generate a standard response result for microservice.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
NewError = src.NewError
)
Functions ¶
func SetConstructor ¶
func SetConstructor(constructor Constructor)
SetConstructor set a function used to creates a constructor function. It's not a routine-safe method, You should call it when service start. Do not call it in a goroutine.
Types ¶
type Constructor ¶
type Constructor = src.ResultConstructor
Constructor is an alias for src.Defaulter.
type Result ¶
Result is an alias for src.Result.
func With ¶
With create and return a response with any data.
Example ¶
res := With(map[string]int{"id": 1})
Output: {"data":{"id":1},"type":"object","errno":0,"error":"success"}
func WithCodeError ¶
WithCodeError create and return a response with specified code and message.
Example ¶
res := WithCodeError(1002, "1002 error")
Output: {"type":"error","errno":1002,"error":"1002 error"}
func WithEmpty ¶
func WithEmpty() Result
WithEmpty create and return a response without data field.
Example ¶
res := WithEmpty()
Output: {"type":"object","errno":0,"error":"success"}
func WithError ¶
WithError create and return an error response with specified error.
Example ¶
res := WithError(NewError(1001, "1001 error"))
Output: {"type":"error","errno":1001,"error":"1001 error"}
func WithList ¶
WithList create and return a list response with specified items.
Example ¶
res := WithList(items)
Output: {"data":[{"id":1},{"id":2},{"id":3}],"type":"list","errno":0,"error":"success"}
func WithPaging ¶
WithPaging create and return a paging response with specified items and paginator.
Example ¶
res := WithPaging(items, 31, 5, len(items))
Output: {"data":{"body":[{"id":1},{"id":2},{"id":3}],"paging":{"total":31,"size":3,"first":1,"prev":4,"curr":5,"next":6,"last":11}},"type":"paging","errno":0,"error":"success"}
func WithString ¶
WithString create and return a string response.
Example ¶
res := WithString("string result")
Output: string result