config

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package config 实现分层配置加载(DESIGN §2):yaml 文件按序合并 → 环境变量覆盖 → unmarshal 到强类型 → validator 校验。 任何一步失败都在启动期报错(fail-fast);校验失败逐字段聚合后一次性报出, 避免"改一个报一个"的多轮启动。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load[T any](opts Options) (T, error)

Load 按 Options 加载配置到 T。返回的错误一律是 *apperr.Error: 校验失败时 Details 逐字段聚合违反的规则(键为 koanf 键路径,如 db.pool_size), 报错不携带字段值——配置里可能有密钥。

T 通常是 struct(字段用 koanf tag 对应键名、validate tag 声明约束); 非 struct 的 T(如 map)跳过校验。

func MustLoad

func MustLoad[T any](opts Options) T

MustLoad 是 Load 的 panic 版本,供 main 顶层 fail-fast 使用。

Types

type Options

type Options struct {
	// Files 按序加载合并:后面的文件覆盖前面的同名键(深合并)。
	Files []string
	// EnvPrefix 非空时启用环境变量覆盖,优先级高于全部文件。
	// 前缀 "APP"(尾部下划线可省)匹配 APP_ 开头的变量;键名去前缀后
	// 转小写,双下划线映射为层级分隔符:APP_FOO__BAR → foo.bar。
	// 单下划线保留在键名内(APP_DB__POOL_SIZE → db.pool_size)。
	// 留空则完全不读环境变量,避免把无关变量吸进配置。
	EnvPrefix string
	// Optional 为 true 时静默跳过 Files 中不存在的文件;
	// 为 false 时文件缺失报错(缺失项聚合后一次性报出)。
	Optional bool
}

Options 配置加载来源。

Jump to

Keyboard shortcuts

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