Go to load tabular data with JSON

Source: Internet
Author: User

Support Hot Reload reload, but there will be some problems, the following note has written

Package Tableimport ("Runtime/debug")//intarray int type arrayType Intarray []int//floatarray Float32 Type arraytype Floatarray []float32//Stringarray Array of string typesType Stringarray []stringtype iTableInterface{load () error reload () error}var(tablelist []itable//mfcity Formmfcity = &mfcitytable{file:".. /data/mfcity.json"}    //citybattlecreature FormCitybattlecreature = &citybattlecreaturetable{file:".. /data/citybattlecreature.json"}) Func init () {tablelist=[]itable{mfcity, Citybattlecreature,}}//Load loads all tablesfunc Load () { for_, V: =Range Tablelist {ifE: = V.load (); Nil! =e {Panic (E.error ()) }}}//Reload Reload All tables//Description://1, Reload table will not reduce the number of bars, such as a table originally has 100, and then to change to 99, Reload finished or 100//2, Reload will not change the length of the array, can only change the value, [one-way] and then the table changed to [2,2],reload after the actual [2,2,3]func Reload () {//intermediate handling unpredictable mistakes must be restored back.defer func () {ifERR: = Recover (); Nil! =Err {log. Error ("[Table.reload]%s", Debug. Stack ())}} () for_, V: =Range Tablelist {ifE: = V.reload (); Nil! =e {log. Error (E.error ()) }}}//deepcopy Deep Copy//to pass in two pointers, do not pass a valueFunc deepcopy (DST, SRCInterface{}) error {varbuf bytes. BufferifERR: = Gob. Newencoder (&BUF). Encode (SRC); Err! =Nil {returnErr}returngob. Newdecoder (bytes. Newbuffer (BUF. Bytes ())). Decode (DST)}

Table code

Package Tableimport ("Runtime/debug")//mfcitydata Single DataType Mfcitydatastruct{IDint' JSON:"ID"' Cityint' JSON:" City"' Lv intarray ' JSON:"LV"' Taskcommon []intarray ' JSON:"Taskcommon"`}//mfcitytable FormType mfcitytablestruct{filestringDataMap map[int]mfcitydata}//Load loadingFunc (Table *mfcitytable) Load () error {ifNil = =table. DataMap {table. DataMap= Make (map[int]mfcitydata)} temp:= Make ([]mfcitydata,0)    ifERR: = util. Loadjsonconfig (Table.file, &temp); Nil! =Err {returnErr} for_, V: =Range Temp {table. Datamap[v.id]=v}returnNil}//Reload re-form//Reload does not do decrement, only increment and changeFunc (Table *mfcitytable) Reload () error {//intermediate handling unpredictable mistakes must be restored back.defer func () {ifERR: = Recover (); Nil! =Err {log. Error ("[Mfcitytable.reload]%s", Debug. Stack ())}} () Temp:= Make ([]mfcitydata,0)    ifERR: = util. Loadjsonconfig (Table.file, &temp); Nil! =Err {returnErr} for_, V: =Range Temp {//Existing to modify the value, add a direct increase        ifData, OK: =table. Datamap[v.id]; OK {deepcopy (&data, &v)}Else{table. Datamap[v.id]=v}}returnNil}//GetByID Search by IDFunc (Table *mfcitytable) GetByID (IDint) (*mfcitydata,BOOL) {V, OK:=table. Datamap[id]return&V, OK}

Go to load tabular data with JSON

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.