Golang parsing the build configuration file using Yaml format

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Or that Golang monitoring agent those things, now the function is almost, but the lack of signal processing and daemon service. The program is almost written, but what about the configuration file? Does it have to be written in the. go file?


I have not yet been able to understand the seamless reload mode of Golang ... If it is written in the configuration file it is very simple, there is a goroutine notify monitoring configuration file or to receive a signal to re-initialize the task.

The article wrote a little messy, welcome to spray! In addition, the article continues to update, please go to the original address to view the update http://xiaorui.cc/?p=3012


Now the main profile format has so many, XML, Yaml, config ... xml even, too bad, too dirt, too cumbersome ... config is the Mysql,apache my.cnf format, this format is suitable for functional layering, not suitable for writing peer configuration.


Yaml is my favorite configuration format, like Ansible, Saltstack, puppet, all using YAML for configuration format. The platform system I developed in my previous company was in YAML format.  Concise, full of tension!!! I only use the YAML format in Python, and of course, in Golang, this is the preferred configuration language. It's nonsense again.

Say Golang about Yaml's library package in GitHub can find several, but some of the YAML function package light look at that wonderful library package name feel not reliable. Speaking of which, I have to spray golang management of the library package, you can not be the whole similar pypi services ... Honestly, I'm really scared. The author submits a new feature and then causes the entire library package to be disabled.

This is the go YAML package that I use, and star's focus value is not low. Https://github.com/go-yaml/yaml  

Nonsense not much to say, directly on the example of Yaml. I used the YAML.V2 library in the code.

As with Golang, the format of YAML needs to be reflected in the struct structure as well as the JSON structure. The first letter of the field in the struct is preferably uppercase, and YAML string does not matter.

Python <textarea wrap= "soft" class= "Crayon-plain print-no" data-settings= "readonly style="-MOZ-TAB-SIZE:4; -o-tab-size:4; -webkit-tab-size:4; Tab-size:4; FONT-SIZE:12PX!important; line-height:15px!important; " > #http://xiaorui.ccpackage mainimport ("FMT" "Log" "Gopkg.in/yaml.v2") var data = ' Blog:xiaorui.ccbest_authors: [" Fengyun "," Lee "," Park "]desc:counter:521 plist: [3, 4] ' type T struct {Blog stringauthors []string ' Yaml: ' best_authors , flow "' Desc struct {Counter int ' yaml:" Counter "' Plist []int ' Yaml:", Flow "'}}func main () {t: = t{}//Parse the string in yaml form into St Ruct Type err: = Yaml. Unmarshal ([]byte (data), &t)//Modify the record t inside the struct. Blog = "This is Blog" t.authors = append (T.authors, "myself") T.desc.counter = 99fmt. PRINTF ("---t:\n%v\n\n", t)//Convert to Yaml string type D, err: = Yaml. Marshal (&t) if err! = Nil {log. Fatalf ("Error:%v", err)}fmt. PRINTF ("---t dump:\n%s\n\n", string (d))} </textarea>
1234567891011121314151617181920212223242526272829303132333435363738394041424344 #http://xiaorui.cc PackageMainImport ("FMT""Log""Gopkg.in/yaml.v2")varData = `Blog: Xiaorui.ccbest_authors: ["Fengyun","Lee","Park"]desc:  counter: 521  plist: [3, 4]`type T struct {BlogstringAuthors []string `Yaml:"Best_authors,flow"`Desc    struct {Counterint   `Yaml:"Counter"`Plist   []int `Yaml:", Flow"`}}funcMain() {T := T{}//PutYamlThe form string is parsed intostructTypeErr := Yaml.Unmarshal([]byte(Data), &T)//ModifystructThe records inside.T.Blog = "This is Blog"T.Authors = Append(T.Authors, "Myself")T.Desc.Counter = AboutFMT.Printf("---t:\n%v\n\n", T)//Converted IntoYamlString typeD, Err := Yaml.Marshal(&T)if Err != Nil {Log.Fatalf("Error:%v", Err)}FMT.Printf("---t dump:\n%s\n\n", string(D))}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.