如何获得 packSOC 和totalKw 的总和和平均值?
我使用 dynamoDB 作为数据库,但据我了解 dynamoDB 不支持聚合。如何获得 packSOC 和totalKw 的平均值和总和。[不可能在结果上使用for 循环,因为结果中有如此多的数据]。
type ValidationModel struct {
Name string `json:"name" valid:"alpha,required~Name is required"`
Email string `json:"email" valid:"email~Enter a valid email.,required~Email is required."`
Password string `json:"password" valid:"required~Password is required"`
}
validationModel := ValidationModel{}
json.NewDecoder(r.Body).Decode(&validationModel)
_, err := govalidator.ValidateStruct(validationModel)
Run Code Online (Sandbox Code Playgroud)
首先,我使用govalidator验证请求正文。
type UserModel struct {
ID bson.ObjectId `json:"_id" bson:"_id"`
Name string `json:"name" bson:"name"`
Email string `json:"email" bson:"email"`
Password string `json:"password,omitempty" bson:"-"`
PasswordHash string `json:"-" bson:"passwordHash"`
Salt string `json:"-" bson:"salt"`
Token string `json:"token,omitempty" bson:"-"`
}
user := models.UserModel{}
json.NewDecoder(r.Body).Decode(&user)
fmt.Println(user)
Run Code Online (Sandbox Code Playgroud)
在验证请求之后,我再次将请求主体解码为用户struct,但已使用validationModel读取了请求主体一次,因此当我尝试再次将其解码为用户时,它没有提供任何值。
我可以在这里想到两种解决方案:
将请求正文存储在一个单独的变量中,并使用该变量两次。
复制用户中的validationModel值。
但是我对实现这些方法一无所知,最好采用哪种方法。还是可以实施其他解决方案?
提前致谢。