Documentation
¶
Overview ¶
Package consts defines application-wide constants including context keys, character sets, metadata keys, and social platform identifiers.
This package provides:
- Context key constants for storing/retrieving values
- Character sets and encoding definitions
- Metadata field name constants
- Social platform identifiers
- Common application constants
Context Keys ¶
Standard keys for storing values in context.Context:
const (
GinContextKey = "GinContext" // Gin context
UserKey = "UserID" // User ID
UsernameKey = "Username" // Username
TraceIDKey = "TraceID" // Trace ID for logging
SpanIDKey = "SpanID" // Span ID for tracing
)
Usage with ctxutil:
import "github.com/ncobase/ncore/consts" import "github.com/ncobase/ncore/ctxutil" ctx = ctxutil.SetValue(ctx, consts.UserKey, "user-123") userID := ctxutil.GetValue(ctx, consts.UserKey)
Character Sets ¶
Predefined character sets for various purposes:
consts.Numeric // "0123456789" consts.Alphabetic // "a-zA-Z" consts.Alphanumeric // "a-zA-Z0-9" consts.AlphanumericSymbol // "a-zA-Z0-9!@#$%"
Usage for validation or generation:
import "strings"
func isNumeric(s string) bool {
return strings.ContainsOnly(s, consts.Numeric)
}
Metadata Keys ¶
Standard metadata field names:
consts.CreatedAt // "created_at" consts.UpdatedAt // "updated_at" consts.DeletedAt // "deleted_at" consts.CreatedBy // "created_by" consts.UpdatedBy // "updated_by"
Use for consistent field naming:
type Model struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
Social Platforms ¶
Identifiers for social authentication providers:
consts.PlatformWeChat // "wechat" consts.PlatformGoogle // "google" consts.PlatformGitHub // "github" consts.PlatformFacebook // "facebook"
Usage in OAuth flows:
switch provider {
case consts.PlatformGoogle:
return googleAuth.Authenticate(token)
case consts.PlatformGitHub:
return githubAuth.Authenticate(token)
}
Best Practices ¶
- Use package constants instead of string literals
- Reference consts for consistent naming
- Add new constants to appropriate categories
- Document custom constant additions
- Use typed constants where appropriate
- Avoid duplicating constant values
Index ¶
Constants ¶
const ( Number = "0123456789" // Numbers Lowercase = "abcdefghijklmnopqrstuvwxyz" // Lowercase letters Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // Uppercase letters Symbol = "!#$%&()*+,-./:;<=>?@[]^_`{|}~" // Symbols NumLower = Number + Lowercase // Numbers + Lowercase letters NumUpper = Number + Uppercase // Numbers + Uppercase letters LowerUpper = Lowercase + Uppercase // Lowercase + Uppercase letters NumLowerUpper = Number + Lowercase + Uppercase // Numbers + Lowercase + Uppercase letters All = NumLowerUpper + Symbol // Combination of all URLSafeBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" )
Character sets
const ( PrimaryKey = NumLowerUpper PrimaryKeySize = 16 )
const ( // SocialWechat Wechat SocialWechat = "wechat" // SocialQQ QQ SocialQQ = "qq" // SocialWeibo Weibo SocialWeibo = "weibo" // SocialGithub Github SocialGithub = "github" )
const AuthorizationKey string = "Authorization"
AuthorizationKey Authorization header key
const BearerKey string = "Bearer "
BearerKey Bearer token prefix
const GinContextKey = "gin-context"
GinContextKey gin context key
const SpaceKey string = "x-md-sid"
SpaceKey global space id SpaceKey = XMdDomainKey
const TokenKey string = "x-md-token"
TokenKey global token
const TotalKey string = "x-md-total"
TotalKey result total with response
const TraceKey string = "x-md-trace"
TraceKey global trace id
const UserEmailKey = "x-md-email"
UserEmailKey global user email
const UserIsCertifiedKey = "x-md-is-certified"
UserIsCertifiedKey global user is certified
const UserKey string = "x-md-uid"
UserKey global user id
const UserStatusKey = "x-md-status"
UserStatusKey global user status
const UsernameKey string = "x-md-uname"
UsernameKey global username
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
This section is empty.