result

package module
v1.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2025 License: Apache-2.0 Imports: 3 Imported by: 5

README

Result

Generate standard result structure for micro service.

import "gitee.com/go-libs/result"

Succeed

{
    "code" : 0, 
    "message" : "succeed", 
    "data": {
        "key": "value"
    }
}

Failed

{
    "code" : 401, 
    "message" : "Unauthorized"
}

Accept

  • Error result.Error(code, message)
  • Object result.Data(any) result.Success()
  • List
  • Paging
  • String

Documentation

Overview

Package result generate a standard response result for microservice.

Index

Examples

Constants

This section is empty.

Variables

View Source
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 Error

type Error = src.Error

Error is an alias for src.Error.

type ErrorGetter

type ErrorGetter = src.ErrorGetter

ErrorGetter is an alias for src.ErrorGetter.

type Result

type Result = src.Result

Result is an alias for src.Result.

func New

func New() Result

New creates a response result instance.

func With

func With(data any) Result

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

func WithCodeError(code int, message string) Result

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

func WithError(err error) Result

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

func WithList(items any) Result

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

func WithPaging(items any, total int64, page, size int) Result

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

func WithString(str string) Result

WithString create and return a string response.

Example
res := WithString("string result")
Output:

string result

Directories

Path Synopsis
src

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL