conf

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: Apache-2.0 Imports: 8 Imported by: 6

README

conf

默认从 sniper.toml 加载配置。虽然 sniper.toml 支持复杂的数据结构, 但框架要求只能设置 k-v 型配置。目的是为了跟环境变量相兼容。

sniper.toml 中的所有配置项都可以使用环境变量覆写。

如果配置文件不在项目根目录,则可以通过环境变量 CONF_PATH 指定。

框架还会自动监听 sniper.toml 内容变更,发现变更会自动热加载。

最后,配置跟环境变量一样,不区分大小写字母。

示例

import "github.com/learninto/goutil/conf"

b := conf.GetBool("IS_SHUTTING_DOWN")

Documentation

Overview

Package conf 提供最基础的配置加载功能

Index

Constants

This section is empty.

Variables

View Source
var (
	// Hostname 主机名
	Hostname = "localhost"
	// AppID 获取 APP_ID
	AppID = "localapp"
	// IsDevEnv 开发环境标志
	IsDevEnv = false
	// IsUatEnv 集成环境标志
	IsUatEnv = false
	// IsProdEnv 生产环境标志
	IsProdEnv = false
	// Env 运行环境
	Env = "dev"
	// Zone 服务区域
	Zone = "sh001"
)

Functions

func Get

func Get(key string) string

Get 获取字符串配置

func GetBool

func GetBool(key string) bool

GetBool 获取配置布尔配置

func GetDuration

func GetDuration(key string) time.Duration

GetDuration 获取时间配置

func GetFloat64

func GetFloat64(key string) float64

GetFloat64 获取浮点数配置

func GetInt

func GetInt(key string) int

GetInt 获取整数配置

func GetInt32

func GetInt32(key string) int32

GetInt32 获取 int32 配置

func GetInt32s

func GetInt32s(key string) (s []int32, err error)

GetInt32s 获取数字列表 1,2,3 => []int32{1,2,3}

func GetInt64

func GetInt64(key string) int64

GetInt64 获取 int64 配置

func GetInt64s

func GetInt64s(key string) (s []int64, err error)

GetInt64s 获取数字列表

func GetStrings

func GetStrings(key string) (s []string)

GetStrings 获取字符串列表

func GetTime

func GetTime(key string, args ...string) time.Time

GetTime 查询时间配置 默认时间格式为 "2006-01-02 15:04:05",conf.GetTime("FOO_BEGIN") 如果需要指定时间格式,则可以多传一个参数,conf.GetString("FOO_BEGIN", "2006")

配置不存在或时间格式错误返回**空时间对象** 使用本地时区

func OnConfigChange

func OnConfigChange(run func())

OnConfigChange 注册配置文件变更回调 需要在 WatchConfig 之前调用

func Set

func Set(key string, value string)

Set 设置配置,仅用于测试

func WatchConfig

func WatchConfig()

WatchConfig 启动配置变更监听,业务代码不要调用。

Types

type Conf

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

func File

func File(name string) *Conf

File 根据文件名获取对应配置对象 目前仅支持 toml 文件,不用传扩展名 如果要读取 foo.toml 配置,可以 File("foo").Get("bar")

func (*Conf) Get

func (c *Conf) Get(key string) string

func (*Conf) GetBool

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

func (*Conf) GetDuration

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

func (*Conf) GetFloat64

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

func (*Conf) GetInt

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

func (*Conf) GetInt32

func (c *Conf) GetInt32(key string) int32

func (*Conf) GetInt32s

func (c *Conf) GetInt32s(key string) (s []int32, err error)

func (*Conf) GetInt64

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

func (*Conf) GetInt64s

func (c *Conf) GetInt64s(key string) (s []int64, err error)

func (*Conf) GetStrings

func (c *Conf) GetStrings(key string) (s []string)

func (*Conf) GetTime

func (c *Conf) GetTime(key string, args ...string) time.Time

func (*Conf) Set

func (c *Conf) Set(key string, value string)

Jump to

Keyboard shortcuts

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