config

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const EnvConfigAddress = "CONFIG_ADDRESS"
View Source
const EnvConfigKEY = "CONFIG_KEY"
View Source
const EnvConfigPath = "CONFIG_PATH"

Variables

This section is empty.

Functions

func GetByTree

func GetByTree(key string) ([]byte, error)

func GetLocalConfigPath

func GetLocalConfigPath() string

GetLocalConfigPath 从命令行 -c 参数获取配置文件路径 使用 sync.Once 保证 flag 只注册一次;若业务方已定义 -c flag 则复用,避免重复注册 panic

Types

type Config

type Config struct {
	*viper.Viper
	// contains filtered or unexported fields
}

Config 配置管理 注意:热重载时会整体替换内部 viper 实例,请使用 Config 提供的 Get* 方法读取配置, 它们内部做了并发保护;直接访问嵌入的 Viper 在热重载场景下不保证并发安全。

func NewConfig

func NewConfig(ConfigPath string) (*Config, error)

func (*Config) AddChangeCallback added in v0.2.0

func (c *Config) AddChangeCallback(callback func())

AddChangeCallback 添加配置变更回调

func (*Config) AllSettings added in v0.3.0

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

func (*Config) DisableHotReload added in v0.2.0

func (c *Config) DisableHotReload() *Config

DisableHotReload 禁用热重载

func (*Config) EnableHotReload added in v0.2.0

func (c *Config) EnableHotReload() *Config

EnableHotReload 启用热重载(文件配置专用)

func (*Config) Get added in v0.3.0

func (c *Config) Get(key string) any

func (*Config) GetBool added in v0.3.0

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

func (*Config) GetBoolWithDefault added in v0.2.0

func (c *Config) GetBoolWithDefault(key string, defaultValue bool) bool

GetBoolWithDefault 获取布尔配置值,如果不存在则返回默认值

func (*Config) GetDuration added in v0.3.0

func (c *Config) GetDuration(key string) time.Duration

func (*Config) GetFloat64 added in v0.3.0

func (c *Config) GetFloat64(key string) float64

func (*Config) GetInt added in v0.3.0

func (c *Config) GetInt(key string) int

func (*Config) GetInt64 added in v0.3.0

func (c *Config) GetInt64(key string) int64

func (*Config) GetIntWithDefault added in v0.2.0

func (c *Config) GetIntWithDefault(key string, defaultValue int) int

GetIntWithDefault 获取整数配置值,如果不存在则返回默认值

func (*Config) GetString added in v0.3.0

func (c *Config) GetString(key string) string

func (*Config) GetStringMap added in v0.3.0

func (c *Config) GetStringMap(key string) map[string]any

func (*Config) GetStringMapString added in v0.3.0

func (c *Config) GetStringMapString(key string) map[string]string

func (*Config) GetStringSlice added in v0.3.0

func (c *Config) GetStringSlice(key string) []string

func (*Config) GetStringWithDefault added in v0.2.0

func (c *Config) GetStringWithDefault(key, defaultValue string) string

GetStringWithDefault 获取字符串配置值,如果不存在则返回默认值

func (*Config) GetWithDefault added in v0.2.0

func (c *Config) GetWithDefault(key string, defaultValue any) any

GetWithDefault 获取配置值,如果不存在则返回默认值

func (*Config) IsHotReloadEnabled added in v0.2.0

func (c *Config) IsHotReloadEnabled() bool

IsHotReloadEnabled 检查是否启用了热重载

func (*Config) IsSet added in v0.3.0

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

func (*Config) ReloadConfig added in v0.2.0

func (c *Config) ReloadConfig() error

ReloadConfig 手动重新加载配置

func (*Config) RemoveAllCallbacks added in v0.2.0

func (c *Config) RemoveAllCallbacks()

RemoveAllCallbacks 清除所有配置变更回调

func (*Config) SetHotReloadInterval added in v0.2.0

func (c *Config) SetHotReloadInterval(intervalSeconds int) *Config

SetHotReloadInterval 设置热重载轮询间隔(仅对Consul配置有效) intervalSeconds: 轮询间隔秒数,-1或0表示禁用热重载,>0表示启用并设置间隔

func (*Config) UnmarshalKey added in v0.3.0

func (c *Config) UnmarshalKey(key string, rawVal any, opts ...viper.DecoderConfigOption) error

func (*Config) Validate added in v0.2.0

func (c *Config) Validate() error

Validate 验证配置

func (*Config) WatchConfig added in v0.2.0

func (c *Config) WatchConfig()

WatchConfig 监听配置变更(只有启用热重载时才生效)

Jump to

Keyboard shortcuts

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