npx skills add https://github.com/cristiano-pacheco/ai-rules --skill go-enum遵循 GO 模块化架构规范生成类型安全的 Go 枚举。
将枚举文件放置在 internal/modules/<module>/enum/<name>_enum.go。
每个枚举文件包含:
New<Type>Enum)String() 方法validate<Type>)注意:当文件中包含带有方法的结构体时,不要添加独立的函数。应使用结构体的私有方法。对于枚举文件,私有的 validate<Type> 函数是可以接受的,因为枚举结构体是一个简单值包装器,没有复杂的方法。
对于 monitor 模块中名为 "ContactType"、值为 "email" 和 "webhook" 的枚举:
Generate type-safe Go enums following GO modular architecture conventions.
Place enums in internal/modules/<module>/enum/<name>_enum.go.
Each enum file contains:
New<Type>Enum)String() methodvalidate<Type>)Note : When a file contains a struct with methods, do not add standalone functions. Use private methods on the struct instead. For enum files, the private validate<Type> function is acceptable because the enum struct is a simple value wrapper without complex methods.
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
package enum
import "github.com/cristiano-pacheco/pingo/internal/modules/monitor/errs"
const (
ContactTypeEmail = "email"
ContactTypeWebhook = "webhook"
)
var validContactTypes = map[string]struct{}{
ContactTypeEmail: {},
ContactTypeWebhook: {},
}
type ContactTypeEnum struct {
value string
}
func NewContactTypeEnum(value string) (ContactTypeEnum, error) {
if err := validateContactType(value); err != nil {
return ContactTypeEnum{}, err
}
return ContactTypeEnum{value: value}, nil
}
func (e ContactTypeEnum) String() string {
return e.value
}
func validateContactType(contactType string) error {
if _, ok := validContactTypes[contactType]; !ok {
return errs.ErrInvalidContactType
}
return nil
}
错误定义位于 internal/modules/<module>/errs/errs.go 并使用 bricks 包中的 errs.New:
package errs
import (
"net/http"
"github.com/cristiano-pacheco/bricks/pkg/errs"
)
var (
ErrInvalidContactType = errs.New("MONITOR_01", "Invalid contact type", http.StatusBadRequest, nil)
)
errs.New 签名:errs.New(code string, message string, httpStatus int, metadata any)
<MODULE>_<NN> — 大写模块前缀,两位顺序号(例如 MONITOR_01,IDENTITY_25)。阅读现有的 errs.go 文件以找到下一个可用的编号。"Invalid contact type")。保持简短且对用户安全。http.StatusBadRequest。nil。ContactType,UserStatus,PKCEMethod)["email", "webhook"],["active", "inactive"])monitor,identity)internal/modules/<module>/errs/errs.go:
ErrInvalid<EnumName> = errs.New("<MODULE>_<NN>", "Invalid <enum name>", http.StatusBadRequest, nil)net/http 和 github.com/cristiano-pacheco/bricks/pkg/errsinternal/modules/<module>/enum/<snake_case_name>_enum.go 创建枚举文件:
github.com/cristiano-pacheco/pingo/internal/modules/<module>/errs<snake_case>_enum.go(例如 contact_type_enum.go,user_status_enum.go)<EnumName><Value>(例如 ContactTypeEmail,UserStatusActive)valid<EnumName>s(非导出,复数形式,例如 validContactTypes)<EnumName>Enum(例如 ContactTypeEnum)New<EnumName>Enum(value string) (<EnumName>Enum, error)validate<EnumName>(value string) error(非导出,单数形式)internal/modules/<module>/errs/errs.go 中的 ErrInvalid<EnumName>internal/modules/<module>/errs/errs.go 以找到下一个顺序错误代码ErrInvalid<EnumName> 添加到 internal/modules/<module>/errs/errs.gointernal/modules/<module>/enum/<snake_case_name>_enum.gomap[string]struct{})value string 字段的枚举结构体New<EnumName>Enum(value string) (<EnumName>Enum, error)String() string 方法validate<EnumName>(value string) error// 验证并包装输入值
contactType, err := enum.NewContactTypeEnum(input)
if err != nil {
return err
}
fmt.Println(contactType.String()) // "email"
// 当值在编译时已知时,直接使用常量
const defaultType = enum.ContactTypeEmail
完成枚举实现后,请确保使用有效和无效值测试构造函数,以确认验证按预期工作。
运行 make lint 和 make nilaway 以确保代码质量且没有空指针问题。
每周安装数
1
仓库
首次出现
1 天前
安全审计
安装于
zencoder1
amp1
cline1
openclaw1
opencode1
cursor1
For an enum named "ContactType" with values "email" and "webhook" in the monitor module:
package enum
import "github.com/cristiano-pacheco/pingo/internal/modules/monitor/errs"
const (
ContactTypeEmail = "email"
ContactTypeWebhook = "webhook"
)
var validContactTypes = map[string]struct{}{
ContactTypeEmail: {},
ContactTypeWebhook: {},
}
type ContactTypeEnum struct {
value string
}
func NewContactTypeEnum(value string) (ContactTypeEnum, error) {
if err := validateContactType(value); err != nil {
return ContactTypeEnum{}, err
}
return ContactTypeEnum{value: value}, nil
}
func (e ContactTypeEnum) String() string {
return e.value
}
func validateContactType(contactType string) error {
if _, ok := validContactTypes[contactType]; !ok {
return errs.ErrInvalidContactType
}
return nil
}
Errors live in internal/modules/<module>/errs/errs.go and use errs.New from the bricks package:
package errs
import (
"net/http"
"github.com/cristiano-pacheco/bricks/pkg/errs"
)
var (
ErrInvalidContactType = errs.New("MONITOR_01", "Invalid contact type", http.StatusBadRequest, nil)
)
errs.New signature: errs.New(code string, message string, httpStatus int, metadata any)
<MODULE>_<NN> — uppercase module prefix, two-digit sequential number (e.g., MONITOR_01, IDENTITY_25). Read the existing errs.go to find the next available number."Invalid contact type"). Keep it short and user-safe.http.StatusBadRequest for invalid enum values.nil for enum validation errors.Identify enum details :
ContactType, UserStatus, PKCEMethod)["email", "webhook"], ["active", "inactive"])monitor, identity)Add error tointernal/modules/<module>/errs/errs.go:
ErrInvalid<EnumName> = errs.New("<MODULE>_<NN>", "Invalid <enum name>", http.StatusBadRequest, nil)net/http and github.com/cristiano-pacheco/bricks/pkg/errs are importedCreate the enum file at internal/modules/<module>/enum/<snake_case_name>_enum.go:
github.com/cristiano-pacheco/pingo/internal/modules/<module>/errs<snake_case>_enum.go (e.g., contact_type_enum.go, user_status_enum.go)<EnumName><Value> (e.g., ContactTypeEmail, UserStatusActive)valid<EnumName>s (unexported, plural, e.g., validContactTypes)<EnumName>Enum (e.g., ContactTypeEnum)New<EnumName>Enum(value string) (<EnumName>Enum, error)validate<EnumName>(value string) error (unexported, singular)ErrInvalid<EnumName> in internal/modules/<module>/errs/errs.gointernal/modules/<module>/errs/errs.go to find the next sequential error codeErrInvalid<EnumName> to internal/modules/<module>/errs/errs.gointernal/modules/<module>/enum/<snake_case_name>_enum.gomap[string]struct{}) with all constantsvalue string fieldNew<EnumName>Enum(value string) (<EnumName>Enum, error)String() string methodvalidate<EnumName>(value string) error private function// Validate and wrap an input value
contactType, err := enum.NewContactTypeEnum(input)
if err != nil {
return err
}
fmt.Println(contactType.String()) // "email"
// Use constants directly when value is known at compile time
const defaultType = enum.ContactTypeEmail
When finished the enum implementation, ensure to test the constructor with valid and invalid values to confirm validation works as expected.
Run make lint and make nilaway to ensure code quality and no nil pointer issues.
Weekly Installs
1
Repository
First Seen
1 day ago
Security Audits
Installed on
zencoder1
amp1
cline1
openclaw1
opencode1
cursor1
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
109,600 周安装
Agently TriggerFlow 状态与资源管理:runtime_data、flow_data 和运行时资源详解
1 周安装
Agently Tools 工具系统详解:Python 代理工具注册、循环控制与内置工具使用
1 周安装
Agently Prompt配置文件技能:YAML/JSON提示模板加载、映射与导出指南
1 周安装
iOS/Android推送通知设置指南:Firebase Cloud Messaging与React Native实现
212 周安装
Agently Playbook:AI智能体开发顶层设计指南与场景路由决策框架
1 周安装
Zig 语言最佳实践指南:类型优先开发、模块结构与核心指令
212 周安装