[Golang] Write from the beginning of the socket Server (4): Put the running parameters into the configuration file (Xml/yaml)

Source: Internet
Author: User
Tags key string readfile
This is a creation in Article, where the information may have evolved or changed. in order to publish our well-written server to the server, we have to build our code to package, so if you want to modify some code later, you need to re-compile the code to package and upload to the server.

Obviously, this is too cumbersome ... As a result, it is common practice to write variables, values, which can be changed frequently in the server run, into the configuration file, which directly allows the program to read parameters from the configuration file and avoid frequent operation of the code.

About the format of the configuration file, here the recommended Yaml and xml~ XML is the traditional configuration file, but I recommend Yaml, he is more humane than XML, but also better to write, about YAML details can be consulted: YAML official website

For example, we can use the server listening port as a variable, write to the configuration file Config.yaml and config, the root directory of the code, so that when we want to replace the server port, as long as the configuration file to modify the port corresponding value can be pulled. The following are the contents of config:


<?xml version= "1.0" encoding= "UTF-8"? ><config1>getconfig</config1><config2>the</ Config2><config3>information</config3><feature1>hereistest1</feature1><feature2 >1024</Feature2><Feature3>Feature23333</Feature3>

Config.yaml content is as follows:


Address:172.168.0.1config1:easyconfig2:  feature1:2  Feature2: [3, 4]port:: 6060config4:isconfig5:atest



The next is to parse them, currently Golang official has not parsed the YAML library, so I recommend the use of third-party Go-yaml package,

The address is as follows: Go-yaml, go get after installing the package, we can parse the file through him:


Parse the file, take out all parameters func Getyamlconfig () map[interface{}]interface{}{data, err: = Ioutil. ReadFile ("Config.yaml")//Convert resolved parameters to map form m: = Make (map[interface{}]interface{}) if err! = Nil {logerr ("Error:%v", err)} Err = Yaml. Unmarshal ([]byte (data), &m) return m}//fetch the corresponding value as required func getelement (key String,themap map[interface{}]interface{}) string {if Value,ok:=themap[key];ok {return value. ( String)}logerr ("Can ' t find the *.yaml") Return ""}




This also gives the code to parse the XML configuration file:


Func getxmlconfig () map[string]string {var t xml. Tokenvar Err Errorkeylst: = make ([]string,6) Valuelst:=make ([]string,6)//fill in map with parsed elements for easy search of Map1:=make (map[string] String) content, err: = Ioutil. ReadFile ("CONFIG. xml") CheckError (ERR) decoder. Newdecoder (bytes. Newbuffer (content)) I:=0j:=0for T, err = decoder. Token (); Err = = Nil; T, Err = decoder. Token () {switch token: = T. (type) {//Process element case XML. Startelement:name: = token. Name.localkeylst[i]=string (name) i=i+1case XML. Chardata:content1: = String ([]byte (token))//valuelst=append (valuelst,content1) valuelst[j]=content1j=j+1}}for Count:=0;count<len (KEYLST); Count++{map1[keylst[count]]=valuelst[count]}return map1}//the function to take out the map is similar to that in Yaml, which is skipped




The results are as follows:





copyright notice: This article for Bo Master original article, without Bo Master permission not reproduced.

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.