response

package
v0.1.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package response 统一响应数据壳、业务错误码约定与响应壳接口(零框架依赖)。 gin / echo 适配器的响应写出函数位于各子模块。

Index

Constants

View Source
const (
	CodeOK    = 0 // 成功
	CodeError = 7 // 业务错误(默认)
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultEnvelope

type DefaultEnvelope struct {
	SuccessMsg string
}

DefaultEnvelope 默认响应壳:{code, data, msg}。 SuccessMsg 自定义成功文案;空则使用默认 "操作成功"。

func (DefaultEnvelope) Failure

func (DefaultEnvelope) Failure(status int, code int, msg string) any

func (DefaultEnvelope) Success

func (e DefaultEnvelope) Success(status int, data any) any

type Envelope

type Envelope interface {
	// Success 成功响应包装。status 为最终 HTTP 状态码(200/201/...),data 为业务数据。
	Success(status int, data any) any
	// Failure 失败响应包装。status 为 HTTP 状态码,code 为业务码,msg 为错误信息。
	Failure(status int, code int, msg string) any
}

Envelope 响应壳接口:运行时适配器在写出前调用。 成功与失败统一走该接口,保证同一服务内响应格式一致; 自定义实现可输出任意风格(RESTful 裸输出、RFC 9457 等)。 文档生成器侧的壳 schema 由 openapi.OptionWithEnvelopeSchema 配对配置。

type Paged

type Paged[T any] struct {
	Items []T   `json:"items"`
	Total int64 `json:"total"`
}

Paged 分页响应

type RawEnvelope

type RawEnvelope struct{}

RawEnvelope 裸响应壳:成功直接输出业务数据(RESTful 风格), 失败输出 {"error": msg}(与 HTTP 语义一致,不做业务码包装)。

func (RawEnvelope) Failure

func (RawEnvelope) Failure(status int, code int, msg string) any

func (RawEnvelope) Success

func (RawEnvelope) Success(status int, data any) any

type Response

type Response[T any] struct {
	Code int    `json:"code"`
	Data T      `json:"data"`
	Msg  string `json:"msg"`
}

Response 统一响应壳:默认业务接口返回 {code, data, msg}。 通过 server.SetEnvelope 可替换为任意自定义壳。

Jump to

Keyboard shortcuts

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