{"id":22332767,"url":"https://github.com/xgfone/gconf","last_synced_at":"2026-02-26T07:04:14.465Z","repository":{"id":57486351,"uuid":"181259738","full_name":"xgfone/gconf","owner":"xgfone","description":"Another extensible and powerful go configuration manager, which is inspired by oslo.config, github.com/micro/go-micro/config and viper.","archived":false,"fork":false,"pushed_at":"2026-01-25T13:39:40.000Z","size":407,"stargazers_count":11,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-01-26T05:53:37.059Z","etag":null,"topics":["argument","argument-parser","cli","cli-parser","config","config-file","configuration","configure","go","go-config","golang","ini","parse","parser","property"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xgfone.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-14T04:21:52.000Z","updated_at":"2026-01-25T13:39:44.000Z","dependencies_parsed_at":"2024-06-19T01:28:44.956Z","dependency_job_id":null,"html_url":"https://github.com/xgfone/gconf","commit_stats":null,"previous_names":[],"tags_count":43,"template":false,"template_full_name":null,"purl":"pkg:github/xgfone/gconf","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xgfone%2Fgconf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xgfone%2Fgconf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xgfone%2Fgconf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xgfone%2Fgconf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xgfone","download_url":"https://codeload.github.com/xgfone/gconf/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xgfone%2Fgconf/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29851221,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-25T22:37:40.667Z","status":"online","status_checked_at":"2026-02-26T02:00:06.774Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["argument","argument-parser","cli","cli-parser","config","config-file","configuration","configure","go","go-config","golang","ini","parse","parser","property"],"created_at":"2024-12-04T04:19:38.179Z","updated_at":"2026-02-26T07:04:14.460Z","avatar_url":"https://github.com/xgfone.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go Config\n\n[![Build Status](https://github.com/xgfone/gconf/actions/workflows/go.yml/badge.svg)](https://github.com/xgfone/gconf/actions/workflows/go.yml)\n[![GoDoc](https://pkg.go.dev/badge/github.com/xgfone/gconf)](https://pkg.go.dev/github.com/xgfone/gconf/v6)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)](https://raw.githubusercontent.com/xgfone/gconf/master/LICENSE)\n![Minimum Go Version](https://img.shields.io/github/go-mod/go-version/xgfone/gconf?label=Go%2B)\n![Latest SemVer](https://img.shields.io/github/v/tag/xgfone/gconf?sort=semver)\n\nAn extensible and powerful go configuration manager, which is inspired by [oslo.config](https://github.com/openstack/oslo.config), [viper](https://github.com/spf13/viper) and [github.com/micro/go-micro/config](https://github.com/micro/go-micro/tree/master/config).\n\n## Install\n\n```shell\n$ go get -u github.com/xgfone/gconf/v6\n```\n\n## Features\n\n- A atomic key-value configuration center.\n- Support kinds of decoders to decode the data from the source.\n- Support to get the configuration data from many data sources.\n- Support to change of the configuration option thread-safely during running.\n- Support to observe the change of the configration options.\n\n## Basic\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/xgfone/gconf/v6\"\n)\n\n// Pre-define a set of options.\nvar opts = []gconf.Opt{\n\tgconf.BoolOpt(\"opt1\", \"opt1 help doc\"),\n\tgconf.StrOpt(\"opt2\", \"opt2 help doc\").D(\"default\"),\n\tgconf.IntOpt(\"opt3\", \"opt3 help doc\").D(123).S(\"o\"), // For short name\n\tgconf.Int32Opt(\"opt4\", \"opt4 help doc\").As(\"opt5\"),  // For alias name\n\tgconf.UintOpt(\"opt6\", \"opt6 help doc\").D(1).V(gconf.NewIntegerRangeValidator(1, 100)),\n\tgconf.Float64Opt(\"opt7\", \"opt7 help doc\").Cli(false),\n}\n\nfunc main() {\n\t// Register the options.\n\tgconf.RegisterOpts(opts...)\n\n\t// Print the registered options.\n\tfor _, opt := range gconf.GetAllOpts() {\n\t\tfmt.Printf(\"Option: name=%s, value=%v\\n\", opt.Name, opt.Default)\n\t}\n\n\t// Add the observer to observe the change of the options.\n\tgconf.Observe(func(optName string, oldValue, newValue any) {\n\t\tfmt.Printf(\"option=%s: %v -\u003e %v\\n\", optName, oldValue, newValue)\n\t})\n\n\t// Update the value of the option thread-safely during app is running.\n\tgconf.Set(\"opt1\", true)\n\tgconf.Set(\"opt2\", \"abc\")\n\tgconf.Set(\"opt3\", \"456\")\n\tgconf.Set(\"opt4\", 789)\n\tgconf.Set(\"opt6\", 100)\n\tgconf.Set(\"opt7\", 1.2)\n\n\t// Get the values of the options thread-safely.\n\tfmt.Println(gconf.Get(\"opt1\"))\n\tfmt.Println(gconf.Get(\"opt2\"))\n\tfmt.Println(gconf.Get(\"opt3\"))\n\tfmt.Println(gconf.Get(\"opt4\"))\n\tfmt.Println(gconf.Get(\"opt6\"))\n\tfmt.Println(gconf.Get(\"opt7\"))\n\n\t// Output:\n\t// Option: name=opt1, value=false\n\t// Option: name=opt2, value=default\n\t// Option: name=opt3, value=123\n\t// Option: name=opt4, value=0\n\t// Option: name=opt6, value=1\n\t// Option: name=opt7, value=0\n\t// option=opt1: false -\u003e true\n\t// option=opt2: default -\u003e abc\n\t// option=opt3: 123 -\u003e 456\n\t// option=opt4: 0 -\u003e 789\n\t// option=opt6: 1 -\u003e 100\n\t// option=opt7: 0 -\u003e 1.2\n\t// true\n\t// abc\n\t// 456\n\t// 789\n\t// 100\n\t// 1.2\n\n\t// TODO ...\n}\n```\n\n## Option Proxy\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/xgfone/gconf/v6\"\n)\n\nfunc main() {\n\t// New the proxy of the option, which will new an option and register them,\n\t// then return the proxy of the option. So you can use the option proxy\n\t// to update and get the value of the option.\n\topt1 := gconf.NewInt(\"opt1\", 111, \"opt1 help doc\")\n\topt2 := gconf.NewDuration(\"opt2\", time.Second, \"opt2 help doc\")\n\n\t// Update the value of the option by the proxy.\n\topt1.Set(\"222\")\n\topt2.Set(\"1m\")\n\n\t// Get the value of the option by the proxy.\n\tfmt.Println(opt1.Get())\n\tfmt.Println(opt2.Get())\n\n\t// Output:\n\t// 222\n\t// 1m0s\n}\n```\n\n## Option Group\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/xgfone/gconf/v6\"\n)\n\n// Pre-define a set of options.\nvar opts = []gconf.Opt{\n\tgconf.StrOpt(\"opt1\", \"opt1 help doc\").D(\"abc\"),\n\tgconf.IntOpt(\"opt2\", \"opt2 help doc\").D(123),\n}\n\nfunc main() {\n\tgroup1 := gconf.Group(\"group1\")  // New the group \"group1\"\n\tgroup2 := group1.Group(\"group2\") // New the sub-group \"group1.group2\"\n\n\tgconf.RegisterOpts(opts...)  // Register opts\n\tgroup1.RegisterOpts(opts...) // Register opts with group1\n\tgroup2.RegisterOpts(opts...) // Register opts with group2\n\n\topt3 := group1.NewFloat64(\"opt3\", 1.2, \"opt3 help doc\")          // For \"group1.opt3\"\n\topt4 := group1.NewDuration(\"opt4\", time.Second, \"opt4 help doc\") // For \"group1.opt4\"\n\topt5 := group2.NewUint(\"opt5\", 456, \"opt5 help doc\")             // For \"group1.group2.opt5\"\n\topt6 := group2.NewBool(\"opt6\", false, \"opt6 help doc\")           // For \"group1.group2.opt6\"\n\n\t/// Update the value of the option thread-safely during app is running.\n\t//\n\t// Method 1: Update the value of the option by the full name.\n\tgconf.Set(\"opt1\", \"aaa\")\n\tgconf.Set(\"opt2\", \"111\")\n\tgconf.Set(\"group1.opt1\", \"bbb\")\n\tgconf.Set(\"group1.opt2\", 222)\n\tgconf.Set(\"group1.opt3\", 2.4)\n\tgconf.Set(\"group1.opt4\", \"1m\")\n\tgconf.Set(\"group1.group2.opt1\", \"ccc\")\n\tgconf.Set(\"group1.group2.opt2\", 333)\n\tgconf.Set(\"group1.group2.opt5\", 444)\n\tgconf.Set(\"group1.group2.opt6\", \"true\")\n\t//\n\t// Method 2: Update the value of the option by the group proxy.\n\tgroup1.Set(\"opt1\", \"bbb\")\n\tgroup1.Set(\"opt2\", 222)\n\tgroup1.Set(\"opt3\", 2.4)\n\tgroup1.Set(\"opt4\", \"1m\")\n\tgroup2.Set(\"opt1\", \"ccc\")\n\tgroup2.Set(\"opt2\", 333)\n\tgroup2.Set(\"opt5\", 444)\n\tgroup2.Set(\"opt6\", \"true\")\n\t//\n\t// Method 3: Update the value of the option by the option proxy.\n\topt3.Set(2.4)\n\topt4.Set(\"1m\")\n\topt5.Set(444)\n\topt6.Set(\"true\")\n\n\t/// Get the values of the options thread-safely.\n\t//\n\t// Method 1: Get the value of the option by the full name.\n\tgconf.Get(\"opt1\")\n\tgconf.Get(\"opt2\")\n\tgconf.Get(\"group1.opt1\")\n\tgconf.Get(\"group1.opt2\")\n\tgconf.Get(\"group1.opt3\")\n\tgconf.Get(\"group1.opt4\")\n\tgconf.Get(\"group1.group2.opt1\")\n\tgconf.Get(\"group1.group2.opt2\")\n\tgconf.Get(\"group1.group2.opt5\")\n\tgconf.Get(\"group1.group2.opt6\")\n\t//\n\t// Method 2: Get the value of the option by the group proxy.\n\tgroup1.Get(\"opt1\")\n\tgroup1.Get(\"opt2\")\n\tgroup1.Get(\"opt3\")\n\tgroup1.Get(\"opt4\")\n\tgroup2.Get(\"opt1\")\n\tgroup2.Get(\"opt2\")\n\tgroup2.Get(\"opt5\")\n\tgroup2.Get(\"opt6\")\n\t//\n\t// Method 3: Get the value of the option by the option proxy.\n\topt3.Get()\n\topt4.Get()\n\topt5.Get()\n\topt6.Get()\n}\n```\n\n## Data Decoder\n\nThe data decoder is a function like `func(src []byte, dst map[string]any) error`, which is used to decode the configration data from the data source.\n\n`Config` supports three kinds of decoders by default, such as `ini`, `json`, `yaml`, and `yml` is the alias of `yaml`. You can customize yourself decoder, then add it into `Config`, such as `Config.AddDecoder(\"type\", NewCustomizedDecoder())`.\n\n## Data Source\n\nA source is used to read the configuration from somewhere the data is. And it can also watch the change the data.\n\n```go\ntype Source interface {\n\t// String is the description of the source, such as \"env\", \"file:/path/to\".\n\tString() string\n\n\t// Read reads the source data once, which should not block.\n\tRead() (DataSet, error)\n\n\t// Watch watches the change of the source, then call the callback load.\n\t//\n\t// close is used to notice the underlying watcher to close and clean.\n\tWatch(close \u003c-chan struct{}, load func(DataSet, error) (success bool))\n}\n```\n\nYou can load lots of sources to update the options. It has implemented the sources based on `flag`, `env`, `file` and `url`. But you can implement other sources, such as `ZooKeeper`, `ETCD`, etc.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/xgfone/gconf/v6\"\n)\n\n// Pre-define a set of options.\nvar opts = []gconf.Opt{\n\tgconf.StrOpt(\"opt1\", \"opt1 help doc\").D(\"abc\"),\n\tgconf.IntOpt(\"opt2\", \"opt2 help doc\").D(123),\n}\n\nfunc main() {\n\tgconf.RegisterOpts(opts...)\n\n\tgroup := gconf.Group(\"group\")\n\tgroup.RegisterOpts(opts...)\n\n\t// Convert the options to flag.Flag, and parse the CLI arguments with \"flag\".\n\tgconf.AddAndParseOptFlag(gconf.Conf)\n\n\t// Load the sources \"flag\" and \"env\".\n\tgconf.LoadSource(gconf.NewFlagSource())\n\tgconf.LoadSource(gconf.NewEnvSource(\"\"))\n\n\t// Load and watch the file source.\n\tconfigFile := gconf.GetString(gconf.ConfigFileOpt.Name)\n\tgconf.LoadAndWatchSource(gconf.NewFileSource(configFile))\n\n\tfor _, opt := range gconf.GetAllOpts() {\n\t\tfmt.Printf(\"%s: %v\\n\", opt.Name, gconf.Get(opt.Name))\n\t}\n\n\tgconf.Observe(func(optName string, oldValue, newValue any) {\n\t\tfmt.Printf(\"%s: %v -\u003e %v\\n\", optName, oldValue, newValue)\n\t})\n\n\ttime.Sleep(time.Minute)\n\n\t// ## Run:\n\t// $ GROUP_OPT2=456 go run main.go --config-file conf.json\n\t// config-file: conf.json\n\t// group.opt1: abc\n\t// group.opt2: 456\n\t// opt1: abc\n\t// opt2: 123\n\t//\n\t// ## echo '{\"opt1\":\"aaa\",\"opt2\":111,\"group\":{\"opt1\":\"bbb\",\"opt2\": 222}}' \u003e conf.json\n\t// opt1: abc -\u003e aaa\n\t// opt2: 123 -\u003e 111\n\t// group.opt1: abc -\u003e bbb\n\t// group.opt2: 456 -\u003e 222\n\t//\n\t// ## echo '{\"opt1\":\"ccc\",\"opt2\":333,\"group\":{\"opt1\":\"ddd\",\"opt2\":444}}' \u003e conf.json\n\t// opt1: aaa -\u003e ccc\n\t// opt2: 111 -\u003e 333\n\t// group.opt1: bbb -\u003e ddd\n\t// group.opt2: 222 -\u003e 444\n\t//\n\t// ## echo '{\"opt1\":\"eee\",\"opt2\":555,\"group\":{\"opt1\":\"fff\",\"opt2\":666}}' \u003e conf.json\n\t// opt1: ccc -\u003e eee\n\t// opt2: 333 -\u003e 555\n\t// group.opt1: ddd -\u003e fff\n\t// group.opt2: 444 -\u003e 666\n}\n```\n\n## Snapshot \u0026 Backup\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/xgfone/gconf/v6\"\n)\n\n// Pre-define a set of options.\nvar opts = []gconf.Opt{\n\tgconf.StrOpt(\"opt1\", \"opt1 help doc\").D(\"abc\"),\n\tgconf.IntOpt(\"opt2\", \"opt2 help doc\").D(123),\n}\n\nfunc main() {\n\tgconf.RegisterOpts(opts...)\n\n\tgroup := gconf.Group(\"group\")\n\tgroup.RegisterOpts(opts...)\n\n\t// Convert the options to flag.Flag, and parse the CLI arguments with \"flag\".\n\tgconf.AddAndParseOptFlag(gconf.Conf)\n\n\t// Load the sources \"flag\" and \"env\".\n\tgconf.LoadSource(gconf.NewFlagSource())\n\tgconf.LoadSource(gconf.NewEnvSource(\"\"))\n\n\t// Load and update the configuration from the backup file which will watch\n\t// the change of all configuration options and write them into the backup\n\t// file to wait to be loaded when the program starts up next time.\n\tgconf.LoadBackupFile(\"config-file.backup\")\n\n\tfmt.Println(gconf.Get(\"opt1\"))\n\tfmt.Println(gconf.Get(\"opt2\"))\n\tfmt.Println(group.Get(\"opt1\"))\n\tfmt.Println(group.Get(\"opt2\"))\n\n\t/// Get the snapshot of all configuration options at any time.\n\t// generation, snapshots := gconf.Snapshot()\n\t// fmt.Println(generation, snapshots)\n\n\t// $ go run main.go\n\t// abc\n\t// 123\n\t// abc\n\t// 123\n\t//\n\t// $ echo '{\"opt1\":\"aaa\",\"opt2\":111,\"group\":{\"opt1\":\"bbb\",\"opt2\":222}}' \u003e config-file.backup\n\t// $ go run main.go\n\t// aaa\n\t// 111\n\t// bbb\n\t// 222\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxgfone%2Fgconf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxgfone%2Fgconf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxgfone%2Fgconf/lists"}