res

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package res 提供统一的 HTTP JSON 响应格式:{"code": .., "msg": .., "data": ..} HTTP 状态码恒为 200,业务成功/失败通过 code 字段区分。

默认 code:成功=0,失败=1。如果项目使用其他约定(如成功=200), 可在 main 初始化时调用 res.SetCodes(200, 1) 全局调整。

Index

Constants

This section is empty.

Variables

View Source
var (
	// CodeOK 业务成功码
	CodeOK = 0
	// CodeFail 通用业务失败码
	CodeFail = 1
)

Functions

func Rfail

func Rfail(c *gin.Context, msg string)

Rfail 返回失败响应(业务码 = CodeFail)

func RfailCode

func RfailCode(c *gin.Context, code int, msg string)

RfailCode 返回带自定义业务码的失败响应

func Rlist

func Rlist(c *gin.Context, total int64, items any)

Rlist 返回分页列表响应:data = { "total": total, "items": items }

func Rsucc

func Rsucc(c *gin.Context, data any)

Rsucc 返回成功响应

func SetCodes

func SetCodes(ok, fail int)

SetCodes 全局调整成功/失败业务码(在 main 初始化时调用一次)

Types

type Body

type Body struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data any    `json:"data"`
}

Body 统一响应结构

Jump to

Keyboard shortcuts

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