Advanced Resource Handling
Complex Resource Management Strategies
Dynamic Resource Selection
//go:embed configs/*.yaml
var configFS embed.FS
func selectConfigByEnvironment(env string) ([]byte, error) {
filename := fmt.Sprintf("configs/%s.yaml", env)
return configFS.ReadFile(filename)
}
Resource Manipulation Techniques
1. Filtering Embedded Files
func filterEmbeddedFiles() {
entries, _ := configFS.ReadDir("configs")
for _, entry := range entries {
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".yaml") {
// Process specific files
}
}
}
2. Recursive Directory Handling
graph TD
A[Recursive Resource Handling] --> B[Directory Traversal]
A --> C[File Filtering]
A --> D[Nested Resource Management]
Recursive File Processing
func processNestedResources(dir string) error {
return fs.WalkDir(configFS, dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
content, _ := configFS.ReadFile(path)
// Process file content
}
return nil
})
}
Advanced Embedding Patterns
Resource Versioning and Management
Technique |
Description |
Use Case |
Conditional Embedding |
Embed resources based on build tags |
Environment-specific builds |
Multiple Embed Directives |
Use multiple embedding strategies |
Complex resource structures |
Runtime Resource Generation |
Combine static and dynamic resources |
Flexible configuration |
Conditional Embedding Example
//go:build production
//go:embed production/*.yaml
//go:build !production
//go:embed development/*.yaml
var configFS embed.FS
Security Considerations
Resource Access Control
func secureResourceAccess(filename string) ([]byte, error) {
// Implement custom access validation
if !isAllowedResource(filename) {
return nil, errors.New("unauthorized resource access")
}
return configFS.ReadFile(filename)
}
Caching Embedded Resources
type ResourceCache struct {
sync.RWMutex
cache map[string][]byte
}
func (rc *ResourceCache) GetResource(name string) ([]byte, error) {
rc.RLock()
if cached, exists := rc.cache[name]; exists {
rc.RUnlock()
return cached, nil
}
rc.RUnlock()
content, err := configFS.ReadFile(name)
if err != nil {
return nil, err
}
rc.Lock()
rc.cache[name] = content
rc.Unlock()
return content, nil
}
Complex Use Cases
Internationalization Resources
//go:embed locales/*.json
var localeFS embed.FS
func loadLocalization(lang string) (map[string]string, error) {
content, err := localeFS.ReadFile(fmt.Sprintf("locales/%s.json", lang))
if err != nil {
return nil, err
}
var translations map[string]string
json.Unmarshal(content, &translations)
return translations, nil
}
LabEx Best Practices
- Implement robust error handling
- Use type-safe resource access methods
- Consider memory and performance implications
- Validate and sanitize embedded resources
Conclusion
Advanced resource handling in Go requires a comprehensive approach that balances flexibility, performance, and security. LabEx recommends carefully designing resource management strategies tailored to specific application needs.