# config **Repository Path**: xgo-core/config ## Basic Information - **Project Name**: config - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-06-17 - **Last Updated**: 2026-07-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # xgo config `gitee.com/xgo-core/config` 是 xgo 体系中的配置加载包。它通过结构体字段标签声明配置来源,启动时读取配置文件或环境变量,反序列化到目标结构体,并注入到 `gitee.com/xgo-core/container`。 完整中文文档见 [README_zh.md](README_zh.md)。 ## 快速开始 ```go type DatabaseConf struct { Host string `json:"host"` Port int `json:"port"` } type AppConf struct { *config.ConfigCenterConf Database *DatabaseConf `from:"json" data:"database" group:"prod" namespace:"default" wire:"primary"` } ``` 对应 JSON 文件: ```text config/database.prod.default.json ``` 初始化: ```go container.MustInjectAs[config.ConfigCenterConfInterface](&AppConf{ ConfigCenterConf: &config.ConfigCenterConf{ Interval: 0, Drivers: config.ConfigCenterDrivers{ Json: config.JsonDriver{Path: "config"}, }, }, }) ctx := context.New() config.New(ctx) db := container.MustGetNamed[*DatabaseConf]("primary") _ = db ``` ## 核心 API - `New(ctx *context.XGoContext)`:立即加载配置,并在 `Interval > 0` 时启动周期刷新。 - `Parse(config any) []*FieldItem`:扫描带 `from` 标签的配置字段。 - `ConfigCenterClientInterface`:driver client 读取配置的统一接口。 - `NewJsonClient(config JsonDriver) *JsonClient`:创建 JSON 文件 client。 - `NewYamlClient(config YamlDriver) *YamlClient`:创建 YAML 文件 client。 - `NewEnvClient() *EnvClient`:创建环境变量 client。 - `(*FieldItem) InjectName() string`:返回容器注入名称,优先使用 `Inject`,为空时使用 `Name`。 字段可通过 `wire:"name"` 指定容器注入名称;加载成功后会调用 `container.MustInjectNamed(name, target)`。未设置 `wire` 时,注入名称回退为结构体字段名。 ## Driver 支持状态 | Driver | 状态 | | --- | --- | | `json` | 已实现 | | `yaml` | 已实现 | | `env` | 已实现 | | `zip` | 占位 | | `nacos` | 占位 | | `etcd` | 占位 | ## 文件命名规则 ```text {data}[.{group}][.{namespace}].json {data}[.{group}][.{namespace}].yaml ``` env driver 使用 `{data}[.{group}][.{namespace}]` 作为环境变量 key。 ## 测试 ```bash go test ./... ```