Go Language parsing INI file

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package main import ("Bufio" "bytes" "IO" "OS" "Unicode") const (Stat_none = Iota Stat_group stat_key stat_value Stat  _comment) type Attr struct {Name string Value string comment string next *attr} type Element struct {element String Attr *attr Next *element} type Decoder struct {state int b byte T bytes. Buffer r io. Bytereader err Error M *element n string} func (d *decoder) getAttr (M *element, E String) string {for n: = M. Attr; Nil! = N; n = n.next {if E = = n.name {return N.value}} return ""} func (D *decoder) getelement (E string, a string) string {fo R N: = D.M; Nil! = N; n = n.next {if E = = n.element {return d.getattr (n, a)}} return ""} func (D *decoder) newattrnextcomment (value string {d.m.attr.comment = value println (value)} func (d *decoder) newattrnext (name string, value string) {Attr: = new (Attr ) attr. Name = Name attr. Value = value if nil = = d.m.attr {Attr.next = nil} else {attr.next = d.m.attr} d.m.attr = attr} func (d *decoder) newelement (name string) {element: = new (Element) element. element = name element. Attr = Nil if nil = = d.m {Element.next = nil} else {element.next = d.m} d.m = element} func (d *decoder) Switchtomap () {for {d.b, D.err = D.r.readbyte () if d.err! = nil {break} switch d.state {case stat_none:if d.b = ' [' {d.state = Stat_group} else if d.b = = '; ' {d.state = stat_comment} else if!unicode. IsSpace (Rune (d.b)) {d.state = Stat_key d.t.writebyte (Byte (d.b))} break case Stat_group:if d.b = '] ' {d.state = Stat_n One d.newelement (d.t.string ()) D.t.reset ()} else if!unicode. IsSpace (Rune (d.b)) {D.t.writebyte (Byte (d.b))} break case Stat_key:if d.b = ' = ' {d.state = Stat_value D.N = d.t.string () D.t.reset ()} else if!unicode. IsSpace (Rune (d.b)) {D.t.writebyte (Byte (d.b))} break case Stat_value:if!unicode. IsSpace (Rune (d.b)) {D.t.writebyte (Byte (d.b))} else {d.state = Stat_none d.newattrnext (D.N, d.t.string ()) D.t.reset ()} Break Case Stat_comment:If!unicode. IsSpace (Rune (d.b)) {D.t.writebyte (Byte (d.b))} else {d.state = Stat_none d.newattrnextcomment (d.t.string ()) D.t.reset ( )} Break Default:d.state = Stat_none Break}}} func (d *decoder) Switchtoreader (R io. Reader) {If RB, OK: = R. (IO. Bytereader); OK {D.R = rb} else {D.R = Bufio. Newreader (R)} d.switchtomap ()} func newdecoder (R io. Reader) *decoder {d: = &decoder{} d.switchtoreader (R) return D} func main () {xmlfile, err: = OS.  Open ("Hh.ini") if nil! = Err {return} defer Xmlfile.close () d: = Newdecoder (xmlfile) println (d.getelement ("AAAA", "AC"))  }

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.