func getObjFuncParam(rw http.ResponseWriter, br []byte) (string, string, map[string]interface{}, error) {input := new(QueryModel)err := json.Unmarshal(br, &input)if err != nil { fmt.Errorf("获取参数错误")}return input.Obj, input.Func, input.Param, nil}
func getFuncResult(rw http.ResponseWriter, obj string, funcName string, Param map[string]interface{}, paramBytes []byte) *model.ResData {expire, ok := cache.AllCacheMap[obj][funcName]_, isExport := Param["isCSV"]if !ok || isExport {result := xxxxx(rw, obj, funcName, Param)return result}// 走缓存key, err := cache.GetQueryKey(obj, funcName, paramBytes)if err != nil {log.Error("cache.GetQueryKey", zap.Error(err))}result, err := cache.GetCacheKey(key)if err != nil {// 分发请求result = xxxxx(rw, obj, funcName, Param)if result.Code == errors.ADMIN_SECUSS {// TODO HANK 根据环境设置时间// 缓存处理cache.SetQueryData(key, result, expire)}}return result}
func GetCacheKey(obj, funcName string, param map[string]interface{}) string {m := map[string]interface{}{"obj": obj,"funcName": funcName,"param": filterSpecialParam(param, true),}bin, _ := json.Marshal(m)hash := fmt.Sprintf("%x", md5.Sum(bin))appzaplog.Info("GetQueryKey Key", zap.String("Cache2Go Field", hash))return hash}// filterParam -过滤特殊的参数做缓存func filterSpecialParam(param map[string]interface{}, isFilter bool) map[string]interface{} {if !isFilter {return param}b, err := json.Marshal(param)if err != nil {return param}newMap := map[string]interface{}{}err = json.Unmarshal(b, &newMap)if err != nil {return param}//删除特殊标识delete(newMap, "userId")delete(newMap, "userName")return newMap}
package my_test_testimport ("encoding/json""fmt""reflect""testing")type MyJson struct {Name string `json:"name"`Age int `json:"age"`Func func() `json:"func"`//Point unsafe.Pointer `json:"point"`//PointInt *int `json:"point_int"`}func TestJson(t *testing.T) {tt := func() {}fmt.Println(reflect.TypeOf(tt))tmp := MyJson{}res, err := json.Marshal(&tmp)fmt.Println(string(res), err)}
报错如下:json: unsupported type: func()json: unsupported type: unsafe.Pointer

json.Marshal的error到底要不要判断?
答:要,所有的程序的返回error都要处理,凡是error,我们最好都进行判断,错误都打错误日志,养成良好的开发习惯,这样查问题解决问题就比较快一些。
文章评论