# cfg **Repository Path**: zhucheer/cfg ## Basic Information - **Project Name**: cfg - **Description**: golang toml 配置文件读取 - **Primary Language**: Go - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2019-10-14 - **Last Updated**: 2023-12-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 一个简单的 toml/yaml 配置读取包 这是一个非常简单的配置读取包,功能非常单一,使用非常简单; ## 安装 ``` gitee.com/zhucheer/cfg ``` ## 使用 ``` cfgHandler, _ := cfg.New("./config.toml") //设置配置文件地址 value := cfgHandler.GetInt("database.connection_max") //读取toml配置节点 ``` ## Nacos 支持 1.首先,我们需要在项目中初始化 Nacos Client。 ``` go clientConfig := *constant.NewClientConfig( constant.WithNotLoadCacheAtStart(true), constant.WithLogDir("./nacos/log"), constant.WithCacheDir("./nacos/cache"), constant.WithRotateTime("1h"), constant.WithMaxAge(6), constant.WithLogLevel("info"), constant.WithNamespaceId(""), constant.WithUsername(), constant.WithPassword() ) configClient, _ := clients.NewConfigClient( vo.NacosClientParam{ ClientConfig: &clientConfig, ServerConfigs: []constant.ServerConfig{ { IpAddr: "106.55.145.120", ContextPath: "/nacos", Port: 8848, Scheme: "http", }, }, }, ) // clientConfig,configClient可以配置成全局变量,在需要使用nacos的地方直接调用 ``` 2.将nacos配置实例化到cfg包的handler ``` go handler := NewNacos(&NacosBase{ ConfigClient: configClient, GroupId: "orange10", DataId: "config.toml", }) key := handler2.GetString("app.name") ```