Documentation
¶
Overview ¶
Package gokart provides focused configuration and state setup for Go applications.
The project-wide admission test and ownership boundary are defined in PHILOSOPHY.md at the repository root. Submodules are independently importable and expose standard-library or upstream types wherever practical.
Index ¶
- func ConfigDir(appName string) (string, error)
- func EnsureConfigDir(appName string, defaultContent []byte) error
- func LoadConfig[T any](paths ...string) (T, error)
- func LoadConfigWithDefaults[T any](defaults T, paths ...string) (T, error)
- func LoadState[T any](appName, filename string) (T, error)
- func MustParseConfig[T any](config map[string]any) T
- func ParseConfig[T any](config map[string]any) (T, error)
- func SaveState[T any](appName, filename string, data T) error
- func StatePath(appName, filename string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConfigDir ¶ added in v0.11.0
ConfigDir returns the app's platform-specific configuration directory, creating it when necessary.
func EnsureConfigDir ¶ added in v0.11.0
EnsureConfigDir creates the app's configuration directory and initializes config.yaml with defaultContent when it does not already exist.
func LoadConfig ¶
LoadConfig loads configuration from the first available file path into type T.
Features:
- Supports multiple config paths (first found wins)
- Automatic environment variable binding
- DOT to UNDERSCORE env key mapping (e.g., db.host → DB_HOST)
Supported formats: JSON, YAML, TOML, HCL, envfile, Java properties
Example:
type Config struct {
DB struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
} `mapstructure:"db"`
}
cfg, err := gokart.LoadConfig[Config]("config.yaml", "config.json")
func LoadConfigWithDefaults ¶
LoadConfigWithDefaults loads configuration with default values pre-populated.
The defaults parameter provides fallback values that will be overridden by values from config files or environment variables.
Example:
defaults := Config{
DB: struct{Host string; Port int}{
Host: "localhost",
Port: 5432,
},
}
cfg, err := gokart.LoadConfigWithDefaults(defaults, "config.yaml")
func LoadState ¶
LoadState loads typed state from the platform user config directory.
Returns zero value and os.ErrNotExist if the file doesn't exist. This allows callers to distinguish between missing file and parse errors.
Example:
state, err := gokart.LoadState[AppState]("myapp", "state.json")
if errors.Is(err, os.ErrNotExist) {
// First run, use defaults
state = AppState{WindowSize: 800}
} else if err != nil {
return err
}
func MustParseConfig ¶ added in v0.10.2
MustParseConfig is ParseConfig for initialization paths where invalid configuration is a programmer error.
func ParseConfig ¶ added in v0.10.2
ParseConfig converts a config map to a typed struct. The config tag names fields, default supplies missing scalar values, and required marks fields that must be present after defaults are applied. Anonymous structs are flattened.
func SaveState ¶
SaveState saves typed state under the platform user config directory.
The file is written as indented JSON for human readability. Directory is created with 0755, files with 0600 permissions.
Example:
type AppState struct {
LastOpened string `json:"last_opened"`
WindowSize int `json:"window_size"`
}
err := gokart.SaveState("myapp", "state.json", AppState{
LastOpened: "/path/to/file",
WindowSize: 1024,
})
Types ¶
This section is empty.
