config

package
v1.5.8 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2026 License: Apache-2.0 Imports: 22 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeTo added in v1.4.5

func DecodeTo(target any) error

DecodeTo 将默认配置实例的完整配置解码到目标值,并校验结构体的 validate tag。

func Exists added in v1.4.5

func Exists(key string) bool

Exists 判断默认配置实例中是否存在指定路径。

func Get

func Get[T cast.Basic](key string) (T, bool)

Get 返回默认配置实例中的基础类型值。

func MustGet

func MustGet[T cast.Basic](key string, defaults ...T) T

MustGet 返回默认配置实例中的基础类型值;路径不存在时优先返回第一个默认值,否则触发 panic。

func Raw added in v1.4.5

func Raw() map[string]any

Raw 返回默认配置实例嵌套数据的深拷贝。

func SetDefault

func SetDefault(c *Config)

SetDefault 替换包级 Get 和 MustGet 使用的默认配置实例。

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) *Config

New 创建一个独立的配置实例。

func (*Config) DecodeTo

func (c *Config) DecodeTo(target any) error

DecodeTo 使用 json tag 将完整配置解码到目标结构体,并解析 duration 与 RFC3339 时间。 解码后使用 validate tag 校验字段。

func (*Config) Exists

func (c *Config) Exists(key string) bool

Exists 判断指定路径是否存在。

func (*Config) Get

func (c *Config) Get[T cast.Basic](key string) (T, bool)

Get 返回指定路径转换后的基础类型值;第二个返回值仅表示路径是否存在。

func (*Config) Load

func (c *Config) Load() error

Load 重新读取全部配置来源,并在校验成功后一次性发布新快照。

func (*Config) MustGet

func (c *Config) MustGet[T cast.Basic](key string, defaults ...T) T

MustGet 返回指定路径转换后的基础类型值;路径不存在时优先返回第一个默认值,否则触发 panic。

func (*Config) Raw

func (c *Config) Raw() map[string]any

Raw 返回嵌套配置数据的深拷贝。

type Option

type Option func(*Config)

func WithFile

func WithFile(filename string, fileType ...string) Option

WithFile 指定配置文件。fileType 可选值为 json 或 yaml;未指定时根据扩展名判断。

func WithFileWatch

func WithFileWatch(enabled bool) Option

WithFileWatch 控制是否在首次成功加载后监听配置文件变化。

Jump to

Keyboard shortcuts

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