分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

Go 用JSON加载表格数据

发布时间:2023-09-06 01:51责任编辑:董明明关键词:暂无标签

支持热重载reload,但会有一些问题,下面注释有写

package tableimport ( ???"runtime/debug")//IntArray int类型数组type IntArray []int//FloatArray Float32类型数组type FloatArray []float32//StringArray string类型数组type StringArray []stringtype iTable interface { ???load() error ???reload() error}var ( ???tableList []iTable ???//MFCity 表格 ???MFCity = &MFCityTable{file: "../data/mfcity.json"} ???//CityBattleCreature 表格 ???CityBattleCreature = &CityBattleCreatureTable{file: "../data/cityBattleCreature.json"})func init() { ???tableList = []iTable{ ???????MFCity, ???????CityBattleCreature, ???}}//Load 加载所有表格func Load() { ???for _, v := range tableList { ???????if e := v.load(); nil != e { ???????????panic(e.Error()) ???????} ???}}//Reload 重新加载所有表格//说明://1、Reload的表不会减少条数,比如A表原来有100条,然后给改成99条,Reload完还是100条//2、Reload不会改变数组长度,只能改变值,[1,2,3]然后表改成[2,2],Reload后实际是[2,2,3]func Reload() { ???//中间处理不可预料得错误一定要恢复回来 ???defer func() { ???????if err := recover(); nil != err { ???????????log.Error("[Table.Reload] %s", debug.Stack()) ???????} ???}() ???for _, v := range tableList { ???????if e := v.reload(); nil != e { ???????????log.Error(e.Error()) ???????} ???}}//DeepCopy 深拷贝//要传入两个指针,不要传值func DeepCopy(dst, src interface{}) error { ???var buf bytes.Buffer ???if err := gob.NewEncoder(&buf).Encode(src); err != nil { ???????return err ???} ???return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)}

表格代码

package tableimport ( ???"runtime/debug")//MFCityData 单个数据type MFCityData struct { ???ID ????????int ???????`json:"id"` ???City ??????int ???????`json:"city"` ???Lv ????????IntArray ??`json:"lv"` ???TaskCommon []IntArray `json:"taskCommon"`}//MFCityTable 表格type MFCityTable struct { ???file ???string ???DataMap map[int]MFCityData}//load 加载func (table *MFCityTable) load() error { ???if nil == table.DataMap { ???????table.DataMap = make(map[int]MFCityData) ???} ???temp := make([]MFCityData, 0) ???if err := util.LoadJSONConfig(table.file, &temp); nil != err { ???????return err ???} ???for _, v := range temp { ???????table.DataMap[v.ID] = v ???} ???return nil}//reload 重新表格//重新加载不会不做减量,只做增量和改变func (table *MFCityTable) reload() error { ???//中间处理不可预料得错误一定要恢复回来 ???defer func() { ???????if err := recover(); nil != err { ???????????log.Error("[MFCityTable.reload] %s", debug.Stack()) ???????} ???}() ???temp := make([]MFCityData, 0) ???if err := util.LoadJSONConfig(table.file, &temp); nil != err { ???????return err ???} ???for _, v := range temp { ???????//已有的要修改值,新增得直接增加 ???????if data, ok := table.DataMap[v.ID]; ok { ???????????DeepCopy(&data, &v) ???????} else { ???????????table.DataMap[v.ID] = v ???????} ???} ???return nil}//GetByID 根据ID查找func (table *MFCityTable) GetByID(id int) (*MFCityData, bool) { ???v, ok := table.DataMap[id] ???return &v, ok}

Go 用JSON加载表格数据

原文地址:https://www.cnblogs.com/mrblue/p/8969195.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved