preferences

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 20, 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/perferences"
)

var (
	config = perferences.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

func GenerateConfig added in v0.0.4

func GenerateConfig() []byte

func Get

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

Get 获取配置项 允许使用点式获取,如:app.name

func GetBool

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

GetBool 获取 Bool 类型的配置信息

func GetFloat64

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

GetFloat64 获取 float64 类型的配置信息

func GetInt

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

GetInt 获取 Int 类型的配置信息

func GetInt64

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

GetInt64 获取 Int64 类型的配置信息

func GetIntSlice

func GetIntSlice(path string) []int

func GetString

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

GetString 获取 String 类型的配置信息

func GetStringMapString

func GetStringMapString(path string) map[string]string

GetStringMapString 获取结构数据

func GetStringSlice

func GetStringSlice(path string) []string

func GetUint

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

GetUint 获取 Uint 类型的配置信息

func IsSet added in v0.2.1

func IsSet(path string) bool

func OpenConfigChangeEvent

func OpenConfigChangeEvent()

OpenConfigChangeEvent 开启监控配置文件⌚️

Types

type ExclusivePreferences

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

func GetExclusivePreferences

func GetExclusivePreferences(prefix string) ExclusivePreferences

func (*ExclusivePreferences) Get

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

func (*ExclusivePreferences) GetBool

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

func (*ExclusivePreferences) GetFloat64

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

func (*ExclusivePreferences) GetInt

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

func (*ExclusivePreferences) GetInt64

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

func (*ExclusivePreferences) GetString

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

func (*ExclusivePreferences) GetStringMapString

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

func (*ExclusivePreferences) GetUint

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

Jump to

Keyboard shortcuts

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