properties

package
v1.43.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2025 License: Apache-2.0 Imports: 12 Imported by: 28

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

Constants

This section is empty.

Variables

View Source
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.

View Source
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

func BindFlags(flags *pflag.FlagSet)

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 Duration added in v1.28.0

func Duration(def time.Duration, keys ...string) time.Duration

func Get

func Get(key string) string

func Int

func Int(def int, key string) int

func On

func On(def bool, keys ...string) bool

func RegisterListener

func RegisterListener(fn func(*Properties))

func Set

func Set(key string, value any)

func String

func String(def string, keys ...string) string

func Update

func Update(props map[string]string)

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) Duration added in v1.28.0

func (p *Properties) Duration(def time.Duration, keys ...string) time.Duration

func (*Properties) Get

func (p *Properties) Get(key string) string

func (*Properties) GetAll

func (p *Properties) GetAll() map[string]string

func (*Properties) Int

func (p *Properties) Int(def int, key string) int

func (*Properties) LoadFile

func (p *Properties) LoadFile(filename string) error

func (*Properties) On

func (p *Properties) On(def bool, keys ...string) bool

func (*Properties) RegisterListener

func (p *Properties) RegisterListener(fn func(*Properties))

func (*Properties) Set

func (p *Properties) Set(key string, value any)

func (*Properties) String

func (p *Properties) String(def string, keys ...string) string

func (*Properties) Update

func (p *Properties) Update(props map[string]string)

func (*Properties) Watch added in v1.27.0

func (p *Properties) Watch() func()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL