How to read ini, json, yaml configuration files by golang
This article Xiaobian introduces in detail for you "how golang reads ini, json, yaml configuration files", the content is detailed, the steps are clear, and the details are handled properly. I hope this "golang how to read ini, json, yaml configuration files" article can help you solve your doubts.
In daily projects, it is inevitable to read various configuration files. Here is a library that can read multiple configuration files, viper
Viper reads ini file config: = viper.New () config.AddConfigPath (". / conf/") / / directory where the file resides config.SetConfigName ("b") / / file name config.SetConfigType ("ini") / / file type if err: = config.ReadInConfig (); err! = nil {if _, ok: = err. (viper.ConfigFileNotFoundError) Ok {fmt.Println ("configuration file not found.")} else {fmt.Println ("configuration file error.")} host: = config.GetString ("redis.host") / / read configuration fmt.Println ("viper load ini:", host)
The b.ini file is as follows
[mysql] username='root'password='123456' [redis] host='127.0.0.1'poet=3306 [mongodb] user='admin'password='admin'viper reads the json file config: = viper.New () config.AddConfigPath (". / conf/") config.SetConfigName ("c") config.SetConfigType ("json") if err: = config.ReadInConfig (); err! = nil {if _, ok: = err. (viper.ConfigFileNotFoundError) Ok {fmt.Println ("configuration file not found.")} else {fmt.Println ("configuration file error..")} version: = config.GetString ("version") origin: = config.GetString ("host.origin") fmt.Println (version) fmt.Println (origin) / / read to map host: = config.GetStringMapString ("host") fmt.Println (host) fmt.Println (host ["origin") "]) fmt.Println (host [" port "]) allSettings: = config.AllSettings () fmt.Println (allSettings)
The c.json file is as follows
{"version": "secret": "footmark", "host": {"origin": "http://www.baidu.com"," port ": 8080}} viper reads the yaml file config: = viper.New () config.AddConfigPath (". / conf/ ") config.SetConfigName (" a ") config.SetConfigType (" yaml ") if err: = config.ReadInConfig (); err! = nil {if _, ok: = err. (viper.ConfigFileNotFoundError) Ok {fmt.Println ("configuration file not found.")} else {fmt.Println ("configuration file error.")} host: = config.GetString ("database.host") fmt.Println ("viper load yml:", host) allSettings: = config.AllSettings () fmt.Println (allSettings)
The a.yaml file is as follows
Database: host: 127.0.0.1 user: root dbname: test pwd: 123456
Common methods of viper
/ / Get (key string): interface {} GetBool (key string): boolGetFloat64 (key string): float64GetInt (key string): intGetIntSlice (key string): [] intGetString (key string): stringGetStringMap (key string): map [string] interface {} GetStringMapString (key string): map [string] stringGetStringSlice (key string): [] stringGetTime (key string): time.TimeGetDuration (key string): time.DurationIsSet (key string): BoolAllSettings (): map [string] interface {} here This article "golang how to read ini, json, yaml configuration files" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.