如何在 delve mozilla rr(record-replay) 重放会话期间显示 decimal.Decimal 的浮点值

how to show float value of decimal.Decimal during delve mozilla rr(record-replay) replay session

如何在delve session中轻松看到decimal.Decimal值的浮点值。 特别是在 dlv replay 由 mozilla rr.

录制的会话中
(dlv) args
underlyingPx = github.com/shopspring/decimal.Decimal {value: ("*math/big.Int")(0xc00101cca0), exp: 3}

(dlv) p underlyingPx
github.com/shopspring/decimal.Decimal {
    value: *math/big.Int {
        neg: false,
        abs: math/big.nat len: 1, cap: 1, [8],},
    exp: 3,}

谢谢

根据source code of Decimal,有一种接收此信息的方法:

// Float64 returns the nearest float64 value for d and a bool indicating
// whether f represents d exactly.
// For more details, see the documentation for big.Rat.Float64
func (d Decimal) Float64() (f float64, exact bool) {
    return d.Rat().Float64()
}

在提供的示例中,只需调用此方法就足够了:

(dlv) p underlyingPx.Float64()

我分叉了 delve 工具并改进了 p 命令:

https://github.com/whunmr/delve/commit/14a12a076b3626b8a36046793508785239a42f55

演示:

(dlv) p im
github.com/shopspring/decimal.Decimal {
    value: *math/big.Int {
        neg: false,
        abs: math/big.nat len: 2, cap: 7, [4670227136653295616,10618058790999],},
    exp: -33,}

value: 0.19586861307716508    <---- this is the added output