preferences

package
v0.2.21 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 15 Imported by: 0

README

一即使全,全既是一

one is all , all is one

本文介绍了一个用于Go语言开发约定的配置文件实现包。许多知名框架(如Laravel、Symfony和Spring)和三方包都约定了一些引导流程,使得用户能够以简便的方式引用和使用它们。 Go语言通过依赖检测(相对于Java class,Go的粒度更为细化)和 init()函数的存在,使得我们可以更加简单地实现约定模型。

本包负责读取配置文件。对于想要使用约定方式进行开发的第三方包,可以遵循以下步骤:

以封装Redis实例包为例,我们在config.toml中定义配置字段,并在Redis包中获取相应的配置:

package redisManager

import (
	"github.com/leancodebox/GooseForum/app/bundles/goose/preferences"
)

var (
	config = preferences.GetExclusivePreferences()
)

var std *redisClient

func init(){
	if configIsSet(config){
		std = getNewClient(config)
    }
}

....

如果使用者想要使用Redis包,只需简单地引入即可。在使用第三方包并设置更多依赖时,约定方式将会变得更加方便。

例如,假设你想要开发一个基于Redis的新队列并将其封装为一个第三方包,那么在使用时只需引入Redis包即可,无需关心前两步约定的设置。在这种情况下,你只需要专注于封装队列的业务逻辑即可:

package queue

import (
	"xxxx/redis"
)

var (
	defaultRedis = redis.GetStdRedis()
)

var queue *queueManager

func init(){
	if defaultRedis != nil {
		queue = getNewQueueManager(defaultRedis)
    }
}

....

要使用这个代码,用户只需在 config.toml 中设置必要的内容。这是一种框架/引导约定,其实现依赖于Go本身的机制,不需要编写任何boot代码(例如spring-boot、laravel-boot、symfony-boot)。 它实现了框架和模块的组合,而且采用了无专门boot代码的约定实现方式。

如果你认同这种开发方式,我们可以一起探讨如何进一步改进它。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddWatch

func AddWatch(event func(e fsnotify.Event))

func All

func All() map[string]any

All returns all loaded settings.

func GenerateConfig added in v0.0.4

func GenerateConfig() ([]byte, error)

func Get

func Get(path string, defaultValue ...any) string

Get returns a string setting and supports dot-separated paths.

func GetBool

func GetBool(path string, defaultValue ...any) bool

GetBool returns a bool setting.

func GetFloat64

func GetFloat64(path string, defaultValue ...any) float64

GetFloat64 returns a float64 setting.

func GetInt

func GetInt(path string, defaultValue ...any) int

GetInt returns an int setting.

func GetInt64

func GetInt64(path string, defaultValue ...any) int64

GetInt64 returns an int64 setting.

func GetIntSlice

func GetIntSlice(path string) []int

GetIntSlice returns an int slice setting.

func GetString

func GetString(path string, defaultValue ...any) string

GetString returns a string setting.

func GetStringMapString

func GetStringMapString(path string) map[string]string

GetStringMapString returns a string map setting.

func GetStringSlice

func GetStringSlice(path string) []string

GetStringSlice returns a string slice setting.

func GetUint

func GetUint(path string, defaultValue ...any) uint

GetUint returns a uint setting.

func IsSet added in v0.2.1

func IsSet(path string) bool

func OpenConfigChangeEvent

func OpenConfigChangeEvent()

OpenConfigChangeEvent 开启监控配置文件⌚️

func Set added in v0.2.9

func Set(path string, value any)

Types

type ExclusivePreferences

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

ExclusivePreferences reads settings below a fixed root path.

func GetExclusivePreferences

func GetExclusivePreferences(prefix string) ExclusivePreferences

GetExclusivePreferences returns a namespaced preferences reader.

func (*ExclusivePreferences) Get

func (itself *ExclusivePreferences) Get(path string, defaultValue ...any) string

Get returns a string setting below the namespace.

func (*ExclusivePreferences) GetBool

func (itself *ExclusivePreferences) GetBool(path string, defaultValue ...any) bool

GetBool returns a bool setting below the namespace.

func (*ExclusivePreferences) GetFloat64

func (itself *ExclusivePreferences) GetFloat64(path string, defaultValue ...any) float64

GetFloat64 returns a float64 setting below the namespace.

func (*ExclusivePreferences) GetInt

func (itself *ExclusivePreferences) GetInt(path string, defaultValue ...any) int

GetInt returns an int setting below the namespace.

func (*ExclusivePreferences) GetInt64

func (itself *ExclusivePreferences) GetInt64(path string, defaultValue ...any) int64

GetInt64 returns an int64 setting below the namespace.

func (*ExclusivePreferences) GetString

func (itself *ExclusivePreferences) GetString(path string, defaultValue ...any) string

GetString returns a string setting below the namespace.

func (*ExclusivePreferences) GetStringMapString

func (itself *ExclusivePreferences) GetStringMapString(path string) map[string]string

GetStringMapString returns a string map setting below the namespace.

func (*ExclusivePreferences) GetUint

func (itself *ExclusivePreferences) GetUint(path string, defaultValue ...any) uint

GetUint returns a uint setting below the namespace.

Jump to

Keyboard shortcuts

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