Golang-encoding Module Learning

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
the usage of encoding in//golang PackageMainImport("bytes""Encoding/binary""Encoding/hex""Encoding/xml""FMT""OS")funcGoxml () {typeAddressstruct{City, State string}//xml ELEMENT nodes add attributes, need to re-customize struct       typeElementwithattrstruct{Attrone string' xml: ' attrone,attr '              Attrtwo string' xml: ' attrtow,attr '       }typePersonstruct{xmlname XML. Name' xml: ' person ' '              Id int' xml: ' id,attr '              Sex string' xml: ' sex,attr '              FirstName string' xml: ' Name>first '              LastName string' xml: ' Name>last '              Age int' xml: ' age '              Height float32' xml: ' Height,omitempty '              EWA elementwithattr' xml:elementwithattr '              //When a child node or attribute is written here (usually not a single string), the "" must be used for encapsulation, otherwise it will not be recognized when reflected              Ewachild string' xml: ' Elementwithattr>ewachild '              married bool Address Comment string' xml: ', comment "'       } V: = &person{id: -, FirstName:"John", LastName:"Doe", Age: the, Sex:"Female"} v.comment ="need more details."       V.address = address{"Hanga Roa","Easter Island"} V.ewa = elementwithattr{"Attribteone","Attributetwo"} V.ewachild ="Elementwithattrchildnode"       ENC: = XML. Newencoder (OS. Stdout) Enc. Indent ("  ","    ")ifERR: = Enc. Encode (v); Err! = Nil {fmt. Printf ("Error:%v\n", err)}}funcMain () {pi: =3.1415926       BUF: = bytes. buffer{}//Using a small encoding, low address corresponding to low byte       Binary. Write (&buf, Binary. Littleendian, &PI)//constant floating-point number is float64 by default       Fmt. Printf ("buf=% #v \ n", buf. Bytes ())varRPI float64 Binary. Read (&buf, Binary. Littleendian, &rpi) fmt. Printf ("rpi=% #v \ n", RPI)//As with Python's binascii library, conversions between 16 and ASCII characters       SRC: ="Go is good language! we are all the same"       //Allocate cache space based on encoded length       DST: = Make ([]byte, Hex. Encodedlen (len (src))) hex. Encode (DST, []byte (SRC)) fmt. Printf ("dst=%v\n", DST) fmt. Printf ("DST (hex) =%s\n", string (DST))//Allocate cache space based on decoding length       Dst2: = Make ([]byte, Hex. Decodedlen (Len (DST))) hex. Decode (Dst2, DST) fmt. Printf ("dst2=%v\n", Dst2) fmt. Printf ("Dst2. (string) =%s\n ", String (DST2)) Goxml ()}
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.