注意针对map时会丢失精度,struct则不会:
package main
import "encoding/json"
type MS struct {
Uid int64
}
func main() {
ms := &MS{
Uid: 1234567891234567891,
}
mss := &MS{}
msj, _ := (ms)
_ = (msj, mss)
print(mss) // 打印 {"Uid":1234567891234567891}
}
精度丢失时:
tmpInstance := &struct {
SignModel
Content string `json:"content,omitempty"`
FloatV float64 `json:"floatV,omitempty"`
ArrV []int64 `json:"arrV,omitempty"`
BoolV bool `json:"boolV"`
}{
SignModel: SignModel{
ApiKey: "EuIWWPXyDAUPmuwFxuRukHczNVkWqwUT",
NonceStr: "sisnfiskjfnkfosmfiskjfnkfiskjfnk",
TimeStamp: 1609826705,
Sign: "123",
},
Content: "我是一段Content文字,我是一段Content文字,我是一段Content文字。。。",
FloatV: 0.1, // 在GenStr函数内转成了:"0.1"
ArrV: []int64{1, 2, 3, 123456789123456789}, // 在GenStr函数内转成了:"[1,2,3]"
BoolV: true, // 在GenStr函数内转成了:"true"
}
interfaceMap := make(map[string]interface{}, 0)
interfaceJson, _ := (tmpInstance)
if err := (interfaceJson, &interfaceMap); err != nil {
("[gateway] VerifySign (interfaceJson, &interfaceMap) err: %v", err)
}
最后interfaceMap里的ArrV参数的第四项是123456789123456780,损失了精度
可以使用 Decode 代替unmarshall来解决
dec := ((str))
()//关键步骤
_ = (&mapS)
(mapS)