一键导入
go-interfaces
Diseño de interfaces y contratos en Go. Usar al definir APIs extensibles, preparar código para testing, o cuando se necesita bajo acoplamiento.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Diseño de interfaces y contratos en Go. Usar al definir APIs extensibles, preparar código para testing, o cuando se necesita bajo acoplamiento.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | go-interfaces |
| description | Diseño de interfaces y contratos en Go. Usar al definir APIs extensibles, preparar código para testing, o cuando se necesita bajo acoplamiento. |
| allowed-tools | Read, Grep, Glob |
Diseño de interfaces, contratos y abstracción en Go.
// ✅ BIEN: Interface pequeña y específica
type Reader interface {
Read(p []byte) (n int, err error)
}
// ❌ MAL: Interface gigante
type DoEverything interface {
Read() / Write() / Close() / ... // 20 métodos
}
// ✅ BIEN: El consumidor define lo que necesita
package validator
type TerminologyService interface {
ValidateCode(ctx context.Context, system, code string) (bool, error)
}
type Validator struct {
terminology TerminologyService // Acepta cualquier implementación
}
// validator/interfaces.go
// TerminologyService - Validación de códigos contra ValueSets
type TerminologyService interface {
ValidateCode(ctx context.Context, system, code, valueSetURL string) (bool, error)
}
// ReferenceResolver - Resolución de referencias FHIR
type ReferenceResolver interface {
Resolve(ctx context.Context, reference string) (interface{}, error)
}
// StructureDefinitionProvider - Obtención de StructureDefinitions
type StructureDefinitionProvider interface {
GetStructureDefinition(ctx context.Context, url string) (*StructureDefinition, error)
}
// validator/phase.go
// PhaseValidator - Cada fase de validación
type PhaseValidator interface {
Name() string
Priority() int
Validate(ctx context.Context, resource []byte, result *ValidationResult) error
}
// PhaseRegistry - Registro de fases
type PhaseRegistry interface {
Register(phase PhaseValidator)
GetPhases() []PhaseValidator
}
// validator/pipeline.go
type ValidationPipeline interface {
Execute(ctx context.Context, resource []byte) (*ValidationResult, error)
}
// validator/treewalker.go
// NodeVisitor - Patrón Visitor para recorrer recursos FHIR
type NodeVisitor interface {
Visit(path string, value interface{}) error
}
// fhirpath/eval/evaluator.go
// FuncRegistry - Registro de funciones FHIRPath
type FuncRegistry interface {
Get(name string) (FuncImpl, bool)
}
// Resolver - Resolución de referencias en expresiones
type Resolver interface {
Resolve(ctx context.Context, reference string) (interface{}, error)
}
// TerminologyService - Para memberOf() y otras funciones
type TerminologyService interface {
ValidateCode(ctx context.Context, system, code, valueSetURL string) (bool, error)
ExpandValueSet(ctx context.Context, valueSetURL string) ([]Code, error)
}
// ProfileValidator - Para conformsTo()
type ProfileValidator interface {
Validate(ctx context.Context, resource interface{}, profileURL string) (bool, error)
}
// fhirpath/types/value.go
// Value - Interface base para todos los valores FHIRPath
type Value interface {
TypeName() string
Equal(other Value) bool
}
// Comparable - Valores que pueden compararse
type Comparable interface {
Value
Compare(other Comparable) (int, error)
}
// Numeric - Valores numéricos
type Numeric interface {
Comparable
AsDecimal() decimal.Decimal
}
// fhirpath/resource.go
// Resource - Interface para recursos FHIR tipados
type Resource interface {
ResourceType() string
GetID() string
}
// Cuando un servicio es opcional, proveer implementación no-op
// NoopTerminologyService - Siempre retorna válido
type NoopTerminologyService struct{}
func (n *NoopTerminologyService) ValidateCode(
ctx context.Context,
system, code, valueSetURL string,
) (bool, error) {
return true, nil // Siempre válido, no hace validación real
}
// Uso en validator
func NewValidator(opts ...Option) *Validator {
v := &Validator{
terminology: &NoopTerminologyService{}, // Default no-op
}
for _, opt := range opts {
opt(v)
}
return v
}
// Usuario puede reemplazar con implementación real
v := NewValidator(
WithTerminologyService(myRealTermService),
)
// Asegurar que un tipo implementa una interface
var _ TerminologyService = (*LocalTerminologyService)(nil)
var _ TerminologyService = (*FHIRTerminologyServer)(nil)
var _ PhaseValidator = (*StructurePhase)(nil)
var _ PhaseValidator = (*ConstraintPhase)(nil)
// Combinar múltiples servicios de terminología
type CompositeTerminologyService struct {
services []TerminologyService
}
func (c *CompositeTerminologyService) ValidateCode(
ctx context.Context,
system, code, valueSetURL string,
) (bool, error) {
for _, svc := range c.services {
valid, err := svc.ValidateCode(ctx, system, code, valueSetURL)
if err == nil && valid {
return true, nil
}
}
return false, nil
}
import "github.com/robertoaraneda/gofhir/r4"
// Interface para trabajar con recursos tipados
type PatientService interface {
GetPatient(ctx context.Context, id string) (*r4.Patient, error)
SavePatient(ctx context.Context, patient *r4.Patient) error
}
// Implementación
type FHIRPatientService struct {
client *http.Client
baseURL string
}
func (s *FHIRPatientService) GetPatient(ctx context.Context, id string) (*r4.Patient, error) {
// Fetch from FHIR server
resp, err := s.client.Get(s.baseURL + "/Patient/" + id)
// ...
}
- [ ] ¿La interface tiene 1-3 métodos?
- [ ] ¿Está definida donde se usa (no donde se implementa)?
- [ ] ¿Incluye context.Context en métodos que pueden bloquear?
- [ ] ¿Es fácil de mockear para testing?
- [ ] ¿Hay implementación no-op para cuando es opcional?
- [ ] ¿Hay verificación de compilación (var _ Interface = (*Type)(nil))?
validator/interfaces.go → Interfaces principales del validator
validator/phase.go → PhaseValidator, PhaseRegistry
validator/pipeline.go → ValidationPipeline
validator/treewalker.go → NodeVisitor
fhirpath/eval/evaluator.go → FuncRegistry, Resolver, TerminologyService
fhirpath/types/value.go → Value, Comparable, Numeric
fhirpath/options.go → ReferenceResolver
Guía para trabajar con StructureDefinitions y ElementDefinitions en FHIR. Usar cuando se necesite entender o manipular definiciones de estructura.
Diseño de arquitectura limpia y modular en Go. Usar al diseñar un nuevo módulo, definir capas y responsabilidades, u organizar código.
Benchmarks y profiling en Go. Usar al medir rendimiento, comparar implementaciones, o identificar bottlenecks.
Patrones de caching y pooling en Go. Usar al implementar caches, reducir allocations, o cachear resultados costosos.
Revisión de código Go. Usar al revisar PRs, verificar calidad de código, o identificar problemas potenciales.
Composición, embedding y extensibilidad en Go. Usar al extender tipos existentes o combinar comportamientos.