Golang Deco Enco

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

Mproto.go

Package Mprotoimport ("bytes" "Encoding/binary" "FMT" "Reflect" "StrConv") const (Pt_gr_luacommand UInt32 = 9299pt_gc_cl Earfacebookbind UInt32 = 32689pt_gc_cleardevicebind UInt32 = 7445pt_gr_bandaccount UInt32 = 29536) type Gr_luacomma nd struct {luacommand string ' key: "21868" '}type gc_clearfacebookbind struct {AccountId int32 ' key: "+" '}func (o *gc_clea Rfacebookbind) decostruct (dat []byte) {buffer: = bytes. Newbuffer (DAT) var fkey int16var Flen int16//take keyfor nil = = binary. Read (buffer, binary. Littleendian, &fkey) {switch Fkey {case 512:{binary. Read (buffer, binary. Littleendian, &flen) binary. Read (buffer, binary. Littleendian, &o.accountid)}break}}}type gr_bandaccount struct {//accountid int32 ' key: "24137" ' bandti Me int64 ' key: ' 23911 ' ' Result string ' key: ' 27966 ' ' Netindex int8 ' key: ' 11487 ' ' Netorder int16 ' key: ' 58 5 "' Something gc_clearfacebookbind ' key:" 1585 "' Array []gc_clearfacebookbind ' key:" 1024x768 "'}func (o *gr_bandaccount) DecOstruct (dat []byte) {buffer: = bytes. Newbuffer (DAT) var fkey int16var Flen int16//take keyfor nil = = binary. Read (buffer, binary. Littleendian, &fkey) {switch Fkey {case 1024:{binary. Read (buffer, binary. Littleendian, &flen) F: = make ([]byte, Flen) binary. Read (buffer, binary. Littleendian, &f) var agc_clearfacebookbind gc_clearfacebookbind = gc_clearfacebookbind{}agc_ Clearfacebookbind.decostruct (f) o.array = append (O.array, agc_clearfacebookbind)}case 1585:{binary. Read (buffer, binary. Littleendian, &flen) F: = make ([]byte, Flen) binary. Read (buffer, binary. Littleendian, &f) var agc_clearfacebookbind gc_clearfacebookbind = gc_clearfacebookbind{}agc_ Clearfacebookbind.decostruct (f) o.something = Agc_clearfacebookbind}case 23911:{binary. Read (buffer, binary. Littleendian, &flen) binary. Read (buffer, binary. Littleendian, &o.bandtime)}case 27966:{binary. Read (buffer, binary. Littleendian, &flen) F: = make ([]byte, Flen) binary. Read (buffer, binary. Littleendian, &f) O.result = string (f)}case 11487:{binary. Read (buffer, binary. Littleendian, &flen) binary. Read (buffer, binary. Littleendian, &o.netindex)}case 585:{binary. Read (buffer, binary. Littleendian, &flen) binary. Read (buffer, binary. Littleendian, &o.netorder)}}}}func encostructsize (O interface{}) int32 {t: = reflect. TypeOf (o) V: = reflect. ValueOf (O) var size int32 = 0for I: = 0; I < T.numfield (); i++ {f: = T.field (i) if f.type.kind () = = reflect. Array | | F.type.kind () = = reflect. Slice {for J: = 0; J < V.field (i). Len (); J + + {if V.field (i). Index (j). Kind () = = reflect. String {Strval, _: = V.field (i). Index (j). Interface (). (string) Size + = Int32 (Len ([]byte (Strval))} else if V.field (i). Index (j). Kind () = = reflect. Int8 {size + = 5} else if V.field (i). Index (j). Kind () = = reflect. Int16 {size + = 6} else if V.field (i). Index (j). Kind () = = reflect. Int32 {size + = 8} else if V.field (i). Index (j). Kind () = = reflect. Int64 {size + = () else if V.field (i). Index (j). Kind () = = reflect. Struct {size + = (encostructsize (V.field (i). IndEx (J). Interface ()))} else {fmt. Printf ("nothing2%6s:%v =%v tag:%s \ n", F.name, F.type, V.field (i). Index (j). Interface (), F.tag)}}} else {if f.type.kind () = = reflect. String {Strval, _: = V.field (i). Interface (). (string) Size + = Int32 (Len ([]byte (Strval))} else if f.type.kind () = = reflect. Int8 {size + = 5} else if f.type.kind () = = reflect. Int16 {size + = 6} else if f.type.kind () = = reflect. Int32 {size + = 8} else if f.type.kind () = = reflect. Int64 {size + = +) Else if f.type.kind () = = reflect. Struct {size + = (encostructsize (V.field (i). Interface ()))} else {fmt. Printf ("nothing1%6s:%v =%v tag:%s%s\n", F.name, F.type, V.field (i). Interface (), F.tag, F.type.kind (). String ())}}}return size}func encostruct (o interface{}) []byte {t: = reflect. TypeOf (o) V: = reflect. ValueOf (o) Buffer: = bytes. Newbuffer ([]byte{}) for I: = 0; I < T.numfield (); i++ {f: = T.field (i)//Keyk, _: = StrConv. Atoi (F.tag.get ("key")) if f.type.kind () = = reflect. Array | | F.type.kind () = = reflect. Slice {for J: = 0; J < V.field (i). Len (); J + + {binary. Write (buffer, binary. Littleendian, Int16 (k)) if V.field (i). Index (j). Kind () = = reflect. String {Strval, _: = V.field (i). Index (j). Interface (). (string)//length binary. Write (buffer, binary. Littleendian, Int16 (Len ([]byte (Strval)))//content binary. Write (buffer, binary. Littleendian, []byte (Strval))} else if V.field (i). Index (j). Kind () = = reflect. Int8 {Int8val, _: = V.field (i). Index (j). Interface (). (int8)//length binary. Write (buffer, binary. Littleendian, Int16 (1))//content binary. Write (buffer, binary. Littleendian, int8 (int8val))} else if V.field (i). Index (j). Kind () = = reflect. Int16 {Int16val, _: = V.field (i). Index (j). Interface (). (int16)//length binary. Write (buffer, binary. Littleendian, Int16 (2))//content binary. Write (buffer, binary. Littleendian, Int16 (int16val))} else if V.field (i). Index (j). Kind () = = reflect. Int32 {Int32val, _: = V.field (i). Index (j). Interface (). (Int32)//length binary. Write (buffer, binary. Littleendian, Int16 (4))//content binary. Write (buffer, binary. Littleendian, Int32 (Int32val))} else if V.field(i). Index (j). Kind () = = reflect. Int64 {Int64val, _: = V.field (i). Index (j). Interface (). (Int64)//length binary. Write (buffer, binary. Littleendian, Int16 (8))//content binary. Write (buffer, binary. Littleendian, Int64 (Int64val))} else if V.field (i). Index (j). Kind () = = reflect. Struct {//length binary. Write (buffer, binary. Littleendian, Int16 (Encostructsize (V.field (i). Index (j). Interface ()))//content binary. Write (buffer, binary. Littleendian, Encostruct (V.field (i). Index (j). Interface ()))} else {fmt. Printf ("Nothing3%6s:%v =%v tag:%s%s\n", F.name, F.type, V.field (i). Index (j). Interface (), F.tag, F.type.kind (). String ())}}} else {binary. Write (buffer, binary. Littleendian, Int16 (k)) Val: = V.field (i). Interface () if f.type.kind () = = reflect. string {Strval, _: = val. (string)//length binary. Write (buffer, binary. Littleendian, Int16 (Len ([]byte (Strval)))//content binary. Write (buffer, binary. Littleendian, []byte (Strval))} else if f.type.kind () = = reflect. Int8 {Int8val, _: = val. (int8)//length binary. Write (buffer, binary. Littleendian, Int16 (1)//content binary. Write (buffer, binary. Littleendian, int8 (int8val))} else if f.type.kind () = = reflect. Int16 {Int16val, _: = val. (int16)//length binary. Write (buffer, binary. Littleendian, Int16 (2))//content binary. Write (buffer, binary. Littleendian, Int16 (int16val))} else if f.type.kind () = = reflect. Int32 {Int32val, _: = Val. (int32)//length binary. Write (buffer, binary. Littleendian, Int16 (4))//content binary. Write (buffer, binary. Littleendian, Int32 (Int32val))} else if f.type.kind () = = reflect. Int64 {Int64val, _: = val. (int64)//length binary. Write (buffer, binary. Littleendian, Int16 (8))//content binary. Write (buffer, binary. Littleendian, Int64 (Int64val))} else if f.type.kind () = = reflect. Struct {//length binary. Write (buffer, binary. Littleendian, Int16 (Encostructsize (val)))//content binary. Write (buffer, binary. Littleendian, Encostruct (val))} else {fmt. Printf ("Nothing4%6s:%v =%v tag:%s%s\n", F.name, F.type, Val, F.tag, F.type.kind (). String ())}}}return buffer. Bytes ()}

Main.go

Package Mainimport ("./mproto" "FMT") func main () {var gr mproto. Gr_bandaccountvar GC Mproto. Gc_clearfacebookbindvar T Mproto. Gr_bandaccount = Mproto. GR_BANDACCOUNT{}GC. AccountId = 4096gr. Bandtime = 111gr. Something.accountid = 32gr.netindex = 4gr.netorder = 5gr. Result = "ABC in vehicle" Gr. Array = Append (Gr. Array, GC) FMT. Println (gr) B: = Mproto. Encostruct (GR) t.decostruct (b) fmt. Println (t)}

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.