Documentation
¶
Overview ¶
Package properties provides a thread-safe properties management system with file loading, hot-reloading, and change notification support.
The package offers a simple key-value store for application configuration with support for loading from properties files, command-line overrides, and dynamic updates with file watching.
Key features:
- Load properties from files (key=value format)
- Command-line property overrides via -P flag
- File watching with automatic hot-reloading
- Thread-safe operations with read-write locks
- Change listeners for reactive configuration
- Type-safe accessors for common data types
Basic usage:
// Load properties from file
err := properties.LoadFile("app.properties")
if err != nil {
log.Fatal(err)
}
// Get property values with defaults
host := properties.String("localhost", "server.host")
port := properties.Int(8080, "server.port")
debug := properties.On(false, "debug.enabled", "debug")
timeout := properties.Duration(30*time.Second, "request.timeout")
// Set properties dynamically
properties.Set("api.key", "secret-key")
// Register change listener
properties.RegisterListener(func(p *properties.Properties) {
log.Println("Properties updated")
})
Command-line usage:
./app -P db.host=localhost -P db.port=5432
Properties file format:
# Comments start with # server.host=localhost server.port=8080 debug.enabled=true request.timeout=30s
The package maintains a global instance for convenience, but you can also create isolated Properties instances for different configuration contexts.
Index ¶
- Variables
- func BindFlags(flags *pflag.FlagSet)
- func Duration(def time.Duration, keys ...string) time.Duration
- func Get(key string) string
- func Int(def int, key string) int
- func On(def bool, keys ...string) bool
- func RegisterListener(fn func(*Properties))
- func Set(key string, value any)
- func String(def string, keys ...string) string
- func Update(props map[string]string)
- type Properties
- func (p *Properties) Duration(def time.Duration, keys ...string) time.Duration
- func (p *Properties) Get(key string) string
- func (p *Properties) GetAll() map[string]string
- func (p *Properties) Int(def int, key string) int
- func (p *Properties) LoadFile(filename string) error
- func (p *Properties) On(def bool, keys ...string) bool
- func (p *Properties) RegisterListener(fn func(*Properties))
- func (p *Properties) Set(key string, value any)
- func (p *Properties) String(def string, keys ...string) string
- func (p *Properties) Update(props map[string]string)
- func (p *Properties) Watch() func()
Constants ¶
This section is empty.
Variables ¶
var Global = &Properties{ m: make(map[string]string), }
Global is the default properties instance used by package-level functions. It's automatically initialized and ready to use.
var LoadFile = func(filename string) error { return Global.LoadFile(filename) }
LoadFile is a convenience function that loads properties from a file into the global properties instance. It's equivalent to Global.LoadFile(filename).
Functions ¶
func BindFlags ¶ added in v1.29.0
BindFlags binds the -P/--properties flag to the given flag set, allowing properties to be set via command line.
Example:
flags := pflag.NewFlagSet("app", pflag.ContinueOnError)
properties.BindFlags(flags)
flags.Parse(os.Args[1:])
// Now you can use: ./app -P key1=value1 -P key2=value2
func RegisterListener ¶
func RegisterListener(fn func(*Properties))
Types ¶
type Properties ¶
type Properties struct {
Reload func() // Function to manually trigger reload
// contains filtered or unexported fields
}
Properties represents a thread-safe key-value store for application configuration. It supports loading from files, dynamic updates, file watching, and change notifications.
func (*Properties) Get ¶
func (p *Properties) Get(key string) string
func (*Properties) GetAll ¶
func (p *Properties) GetAll() map[string]string
func (*Properties) LoadFile ¶
func (p *Properties) LoadFile(filename string) error
func (*Properties) RegisterListener ¶
func (p *Properties) RegisterListener(fn func(*Properties))
func (*Properties) Set ¶
func (p *Properties) Set(key string, value any)
func (*Properties) Update ¶
func (p *Properties) Update(props map[string]string)
func (*Properties) Watch ¶ added in v1.27.0
func (p *Properties) Watch() func()