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