Files
Crimson-Gatekeeper/srv/internal/common/context_variable.go
2026-01-19 00:07:20 +08:00

27 lines
651 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package common
import (
"github.com/gin-gonic/gin"
)
type ctxKey struct {
desc string
}
var (
dataKey = ctxKey{"响应体内容"}
)
// SetData 设置响应数据,可以设置为 nil或者干脆不设置。此时会返回空的响应包装。
func SetData(c *gin.Context, data any) {
c.Set(dataKey, data)
}
// GetData 获取响应数据
// 除了响应包装器,你不应该在任何地方调用该方法。因为响应数据的类型不一。
// 如果你真的对响应数据处理。请在中间件向上下文中塞入数据,在 handler 中处理好。
func GetData(c *gin.Context) any {
data, _ := c.Get(dataKey)
return data
}