Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to read configuration files such as yaml,json,ini

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to read yaml,json,ini and other configuration files, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

In the actual project, there is also an important basic function, which is to read the relevant configuration files. Today let's talk about how Golang reads configuration files such as YAML,JSON,INI.

1. Json usage

JSON should be familiar with, it is a lightweight data exchange format. The hierarchical structure is simple and clear, easy to read and write, but also easy to machine parse and generate.

1. Create a conf.json:

{"enabled": true, "path": "/ usr/local"}

two。 Create a new config_json.go:

Package main

Import ("encoding/json", "fmt", "os")

Type configuration struct {Enabled bool Path string}

Func main () {/ / Open the file file, _: = os.Open ("conf.json")

/ / close the file defer file.Close ()

/ / NewDecoder creates a * Decoder that reads and decodes json objects from file. The decoder has its own buffer and may read part of the json data ahead of time. Decoder: = json.NewDecoder (file)

Conf: = configuration {} / / Decode reads the next json code value from the input stream and stores it in the value pointed to by v err: = decoder.Decode (& conf) if err! = nil {fmt.Println ("Error:", err)} fmt.Println ("path:" + conf.Path)}

After starting the operation, the output is as follows:

D:\ Go_Path\ go\ src\ configmgr > go run config_json.gopath:/usr/local

2. The use of ini

INI file format is an informal standard for configuration files on some platforms or software. it consists of section and key, which is commonly used in Microsoft Windows operating system. The file extension for this configuration file is INI.

1. Create a conf.ini:

[Section] enabled = truepath = / usr/local # another comment

two。 Download the third party library: go get gopkg.in/gcfg.v1

3. Create a new config_ini.go:

Package main

Import ("fmt" gcfg "gopkg.in/gcfg.v1")

Func main () {config: = struct {Section struct {Enabled bool Path string}} {}

Err: = gcfg.ReadFileInto (& config, "conf.ini")

If err! = nil {fmt.Println ("Failed to parse config file:% s", err)} fmt.Println (config.Section.Enabled) fmt.Println (config.Section.Path)}

After starting the operation, the output is as follows:

D:\ Go_Path\ go\ src\ configmgr > go run config_ini.gotrue/usr/local

3. Yaml usage

Yaml may be a bit unfamiliar, but it has become more and more popular recently. That is, a markup language. The hierarchical structure is also very simple and clear, easy to read and write, but also easy to machine parse and generate.

Golang's standard library does not provide us with a standard library to operate yaml for the time being, but there are many excellent third-party libraries on github for us to use.

1. Create a conf.yaml:

Enabled: truepath: / usr/local

two。 Download the third party library: go get gopkg.in/yaml.v2

3. Create a config_yaml.go:

Package main

Import ("fmt"io/ioutil"log"

"gopkg.in/yaml.v2")

Type conf struct {Enabled bool `yaml: "enabled" `/ / yaml:yaml format enabled: attribute is enabled Path string `yaml: "path"`}

Func (c * conf) getConf () * conf {yamlFile, err: = ioutil.ReadFile ("conf.yaml") if err! = nil {log.Printf ("yamlFile.Get err #% v", err)}

Err = yaml.Unmarshal (yamlFile, c) if err! = nil {log.Fatalf ("Unmarshal:% v", err)} return c}

Func main () {var c conf c.getConf () fmt.Println ("path:" + c.Path)}

After starting the operation, the output is as follows:

D:\ Go_Path\ go\ src\ configmgr > go run config_yaml.gopath:/usr/local so much about how to read configuration files such as yaml,json,ini. I hope the above can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report