\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d\u6211\u4eec\u5b66\u4f1a\u4e86\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u6765\u7ba1\u7406\u6211\u4eec\u4f9d\u8d56\u7684\u5305\u3002<\/p> \n
\u914d\u7f6e\u5de5\u5177\u7684\u9009\u62e9<\/h2> \n
\u4f46\u6211\u4eec\u53c8\u9047\u5230\u4e86\u4e00\u4e2a\u95ee\u9898\uff0c\u4e00\u4e2a\u9879\u76ee\u901a\u5e38\u662f\u6709\u5f88\u591a\u914d\u7f6e\u7684\uff0c\u6bd4\u5982PHP\u7684php.ini\u6587\u4ef6\u3001Nginx\u7684server.conf\u6587\u4ef6\uff0c\u90a3\u4e48Golang\u7684\u9879\u76ee\u53c8\u9002\u5408\u4f7f\u7528\u600e\u6837\u7684\u914d\u7f6e\u6587\u4ef6\u5462\uff1f<\/p> \n
\u5176\u5b9e\u73b0\u5728\u6211\u4eec\u6709\u5f88\u591a\u9009\u62e9\uff0c\u6bd4\u5982 JSON\u6587\u4ef6\u3001INI\u6587\u4ef6\u3001YAML\u6587\u4ef6\u548cTOML\u6587\u4ef6\u7b49\u7b49\u3002<\/p> \n
\u5176\u4e2d\u8fd9\u4e9b\u6587\u4ef6\uff0c\u5bf9\u5e94\u7684Golang\u5904\u7406\u5e93\u5982\u4e0b\uff1a<\/p> \n
- \n
- encoding\/json<\/a> -- \u6807\u51c6\u5e93\u4e2d\u7684\u5305\uff0c\u53ef\u4ee5\u5904\u7406JSON\u914d\u7f6e\u6587\u4ef6\uff0c\u7f3a\u70b9\u662f\u4e0d\u80fd\u52a0\u6ce8\u91ca<\/li> \n
- gcfg<\/a> -- \u5904\u7406INI\u914d\u7f6e\u6587\u4ef6<\/li> \n
- toml<\/a> -- \u5904\u7406TOML\u914d\u7f6e\u6587\u4ef6<\/li> \n
- viper<\/a> -- \u5904\u7406JSON, TOML, YAML, HCL\u4ee5\u53caJava properties\u914d\u7f6e\u6587\u4ef6<\/li> \n <\/ul> \n
\u5176\u5b9e\u5173\u4e8e\u600e\u4e48\u9009\u62e9\u53ef\u4ee5\u770b\u770bstackoverflow\u4e0a\u7684\u95ee\u9898How to handle configuration in Go<\/a>\u3002<\/p> \n
toml\u7684\u4f7f\u7528<\/h2> \n
\u6211\u6839\u636e\u81ea\u5df1\u7684\u559c\u597d\u9009\u4e86toml\uff0c\u4e0b\u9762\u5c31\u6765\u8bf4\u4e0btoml\u3002<\/p> \n
\u5148\u6765\u770b\u4e00\u4e2aTOML\u6587\u4ef6\u7684\u4f8b\u5b50\uff1a<\/p> \n
# This is a TOML document.\n\ntitle = "TOML Example"\n\n[owner]\nname = "Tom Preston-Werner"\ndob = 1979-05-27T07:32:00-08:00 # First class dates\n\n[database]\nserver = "192.168.1.1"\nports = [ 8001, 8001, 8002 ]\nconnection_max = 5000\nenabled = true\n\n[servers]\n\n # Indentation (tabs and\/or spaces) is allowed but not required\n [servers.alpha]\n ip = "10.0.0.1"\n dc = "eqdc10"\n\n [servers.beta]\n ip = "10.0.0.2"\n dc = "eqdc10"\n\n[clients]\ndata = [ ["gamma", "delta"], [1, 2] ]\n\n# Line breaks are OK when inside arrays\nhosts = [\n "alpha",\n "omega"\n]<\/code><\/pre> \n
\u5927\u5bb6\u53ef\u4ee5\u770b\u5230\u8fd9\u91cc\u7684\u683c\u5f0f\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u662f\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u7b49\u7b80\u5355\u7c7b\u578b\uff0c\u4e5f\u53ef\u4ee5\u662f\u6570\u7ec4\u3001map\u7b49\u7b49\u590d\u6742\u7684\u7c7b\u578b\u3002<\/p> \n
\u5173\u4e8e\u5177\u4f53\u7684TOML\u8bed\u8a00\u7684\u89e3\u8bf4\u5927\u5bb6\u67e5\u770b\u6587\u6863 toml-lang\/toml<\/a><\/p> \n
\u4e0b\u9762\u6211\u4eec\u518d\u6765\u8bf4\u4e00\u4e0b\uff0c\u5177\u4f53\u7684Golang\u4ee3\u7801\u4e2d\u5982\u4f55\u4f7f\u7528<\/p> \n
\u6211\u4eec\u57fa\u4e8e\u4e0a\u9762\u7684\u914d\u7f6e\u6587\u4ef6\u6765\u5b9a\u4e49Golang\u4e2d\u914d\u7f6e\u7684struct\uff0c\u5982\u4e0b\uff1a<\/p> \n
type tomlConfig struct {\n Title string\n Owner ownerInfo\n DB database `toml:"database"`\n Servers map[string]server\n Clients clients\n}\n\ntype ownerInfo struct {\n Name string\n Org string `toml:"organization"`\n Bio string\n DOB time.Time\n}\n\ntype database struct {\n Server string\n Ports []int\n ConnMax int `toml:"connection_max"`\n Enabled bool\n}\n\ntype server struct {\n IP string\n DC string\n}\n\ntype clients struct {\n Data [][]interface{}\n Hosts []string\n}<\/code><\/pre> \n
\u8fd9\u4e00\u4e9b\u90fd\u5b9a\u4e49\u597d\u4e4b\u540e\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5c06\u6587\u4ef6\u914d\u7f6e\u4e2d\u7684\u5185\u5bb9\u8f6c\u6210Golang\u4e2d\u53ef\u7528\u7684struct\u5b9e\u4f8b\u5373\u53ef\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p> \n
var config tomlConfig\nfilePath := "\/your\/path\/config.toml"\nif _, err := toml.DecodeFile(filePath, &config); err != nil {\n panic(err)\n}<\/code><\/pre> \n
\u8fd9\u6837\u6211\u4eec\u62ff\u5230\u7684config\u5c31\u662f\u62e5\u6709TOML\u6587\u4ef6\u5185\u5bb9\u7684tomlConfig\u7684\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002<\/p> \n
\u914d\u7f6e\u7684\u5355\u4f8b\u6a21\u5f0f<\/h2> \n
\u901a\u5e38\u6765\u8bf4\uff0c\u5728\u4e00\u4e2a\u9879\u76ee\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6\u53ea\u9700\u8981\u89e3\u6790\u4e00\u6b21\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u5305\u4e00\u4e0bconfig\u7684\u89e3\u6790\u3002<\/p> \n
\u4ee3\u7801\u5982\u4e0b\uff1a<\/p> \n
package config\n\nvar (\n cfg * tomlConfig\n once sync.Once\n)\n\nfunc Config() *tomlConfig {\n once.Do(func() {\n filePath, err := filepath.Abs(".\/ch3\/config.toml")\n if err != nil {\n panic(err)\n }\n fmt.Printf("parse toml file once. filePath: %s\\n", filePath)\n if _ , err := toml.DecodeFile(filePath, &cfg); err != nil {\n panic(err)\n }\n })\n return cfg\n}<\/code><\/pre> \n
\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86sync.Once\u7684Do\u65b9\u6cd5\uff0cDo\u65b9\u6cd5\u5f53\u4e14\u4ec5\u5f53\u7b2c\u4e00\u6b21\u88ab\u8c03\u7528\u65f6\u624d\u6267\u884c\u51fd\u6570\u3002<\/p> \n
\u5982\u679conce.Do(f)\u88ab\u591a\u6b21\u8c03\u7528\uff0c\u53ea\u6709\u7b2c\u4e00\u6b21\u8c03\u7528\u4f1a\u6267\u884cf\uff0c\u5373\u4f7ff\u6bcf\u6b21\u8c03\u7528Do \u63d0\u4f9b\u7684f\u503c\u4e0d\u540c\u3002\u9700\u8981\u7ed9\u6bcf\u4e2a\u8981\u6267\u884c\u4ec5\u4e00\u6b21\u7684\u51fd\u6570\u90fd\u5efa\u7acb\u4e00\u4e2aOnce\u7c7b\u578b\u7684\u5b9e\u4f8b\u3002<\/p> \n
\u8fd9\u6837\u6211\u4eec\u5c31\u4fdd\u8bc1\u4e86tomlConfig\u5bf9\u8c61\u662f\u4e00\u4e2a\u5355\u4f8b\u6a21\u5f0f\uff0c\u53ea\u9700\u8981\u89e3\u6790\u4e00\u6b21\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u8c03\u7528\u3002\u8c03\u7528\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p> \n
\/\/ \u914d\u7f6e\u4e2dDB\u7684IP\nfmt.Println(config.Config().DB.Server)\n\/\/ \u914d\u7f6e\u4e2dOwner\u7684\u540d\u5b57\nfmt.Println(config.Config().Owner.Name)<\/code><\/pre> \n
\u914d\u7f6e\u7684\u66f4\u65b0<\/h2> \n
\u5982\u679c\u6211\u4eec\u7684\u9879\u76ee\u662f\u4e00\u4e2a\u5e38\u9a7b\u7684\u9879\u76ee\uff08\u6bd4\u5982http server\uff09\uff0c\u6211\u4eec\u4f1a\u5e0c\u671b\u80fd\u591f\u63d0\u4f9b\u66f4\u65b0\u914d\u7f6e\u7684\u529f\u80fd\uff0c\u5e73\u6ed1\u7684\u66ff\u6362\u6389\u914d\u7f6e\uff0c\u4e0d\u9700\u8981\u91cd\u542f\u9879\u76ee\u3002<\/p> \n
\u5176\u5b9e\u601d\u8def\u5f88\u60f3\u7b80\u5355\uff0c\u6211\u4eec\u53ea\u9700\u8981\u8d77\u4e00\u4e2a\u534f\u7a0b\uff0c\u76d1\u89c6\u6211\u4eec\u5b9a\u4e49\u597d\u7684\u4fe1\u53f7\uff0c\u5982\u679c\u63a5\u6536\u5230\u4fe1\u53f7\u5c31\u91cd\u65b0\u52a0\u8f7d\u914d\u7f6e\u3002<\/p> \n
\u4e0b\u9762\u6211\u4eec\u6765\u5199\u4e0b\uff0c\u66f4\u65b0\u914d\u7f6e\u7684\u4ee3\u7801\uff1a<\/p> \n
s := make(chan os.Signal, 1)\n signal.Notify(s, syscall.SIGUSR1)\n go func() {\n for {\n <-s\n config.ReloadConfig()\n log.Println("Reloaded config")\n }\n }()<\/code><\/pre> \n
\u6211\u4eec\u76d1\u89c6\u4e86syscall.SIGUSR1\u4fe1\u53f7\uff0c\u5176\u503c\u662f30\uff0c\u63a5\u6536\u5230\u4fe1\u53f7\u5c31\u6267\u884cconfig.ReloadConfig()\u65b9\u6cd5\u3002<\/p> \n
\u518d\u6765\u770b\u4e0bconfig\u4e2d\u65b9\u6cd5\u53d8\u52a8\uff1a<\/p> \n
func Config() *tomlConfig {\n once.Do(ReloadConfig)\n cfgLock.RLock()\n defer cfgLock.RUnlock()\n return cfg\n}\n\nfunc ReloadConfig() {\n filePath, err := filep","orderid":"0","title":"Golang\u5b66\u4e60--TOML\u914d\u7f6e\u5904\u7406(\u4e00)","smalltitle":"","mid":"0","fname":"GO","special_id":"0","bak_id":"0","info":"0","hits":"1909","pages":"2","comments":"0","posttime":"2017-12-24 06:07:01","list":"1514066821","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Golang<\/A> \u5b66\u4e60<\/A> --TOML<\/A> \u914d\u7f6e<\/A> \u5904\u7406<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"113.110.148.213","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Golang\u5b66\u4e60--TOML\u914d\u7f6e\u5904\u7406","lastview":"1713993921","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}
- gcfg<\/a> -- \u5904\u7406INI\u914d\u7f6e\u6587\u4ef6<\/li> \n