First off, the package you're using for redis isn't maintained; you should
switch to github.com/gomodule/redigo/redis instead, which will allow you to
remove the c == nil check as that doesn't happen.

In your example you're ignoring error from json.Marshal which could be
hiding a problem, so I would recommend you handle that.

encoding/json should represent float as a json number so I would never
expect what you're seeing but its not clear to me if that is down to how
you are viewing it.

On Tue, 12 Apr 2022 at 04:02, Zhaoxun Yan <yan.zhao...@gmail.com> wrote:

> The scenario is upon receiving an incoming financial quotation, save it as
> a string of json into a Redis service. Sorry but I cannot provide the whole
> code of quotation receiving here, which is very complex with cgo. But the
> code below will help you get a glimpse on what should be going on:
>
> import (
>     "encoding/json"
>     //"errors"
>     "fmt"
>     "time"
>
>     "github.com/garyburd/redigo/redis"
> )
>
> var pool *redis.Pool
>
> type Fvprices struct {
>     P float64 `json:"price"`
>     F float64 `json:"floor"`
>     C float64 `json:"ceiling"`
>     S float64 `json:"settle"`
>     T int64   `json:"time"`
> }
>
> func init() {
>     pool = newPool()
> }
>
> var redisport = "6379"
> var redisip = "127.0.0.1"
> var password = ""
>
> func newPool() *redis.Pool {
>
>     fmt.Println("redis @", redisport, redisip, password)
>     return &redis.Pool{
>         MaxIdle:     4,
>         MaxActive:   50, // max number of connections
>         IdleTimeout: 30 * time.Second,
>
>         Dial: func() (redis.Conn, error) {
>             c, err := redis.DialURL("redis://" + redisip + ":" +
> redisport)
>             if err != nil {
>                 ErrMsg = fmt.Sprintf("redis connection error: %s", err.
> Error())
>                 fmt.Println(time.Now().Format("2006-01-02 15:04:05"),
> ErrMsg)
>                 return nil, err
>             }
>             if _, autherr := c.Do("AUTH", password); autherr != nil {
>                 ErrMsg = fmt.Sprintf("redis password error: %s", err.Error
> ())
>                 fmt.Println(time.Now().Format("2006-01-02 15:04:05"),
> ErrMsg)
>                 return nil, autherr
>             }
>             return c, nil
>         },
>     }
> }
>
> func Upfutureprice(future_id string,
>     future_price, lowerLimitPrice, upperLimitPrice, preSettlementPrice
> float64,
>     updatetime time.Time) {
>
>     c := pool.Get()
>     if c == nil {
>         return
>     }
>     defer c.Close()
>
>     content := Fvprices{
>         P: future_price,
>         F: lowerLimitPrice,
>         C: upperLimitPrice,
>         S: preSettlementPrice,
>         T: updatetime.UnixNano() / 1e6,
>     }
>
>     js, _ := json.Marshal(content)
>
>     if _, err := c.Do("SET", future_id, js); err != nil {
>         fmt.Println("cannot save to redis:", err)
>     }
> }
>
> So obviously until the function "Upfutureprice" everything is correct,
> for all  four prices it receives are in float64 format. After running this
> program for one day, I just browse the redis using
> AnotherRedisDesktopManager via ssh port forwarding, and something strange
> happens as I clicking on various future_id key strings:
>
> {
> price:807
> floor:720.6
> ceiling:881
> settle:"800.8000000000001"
> time:1649726499000
> }
>
> {
> price:"3691.0000000000005"
> floor:3237
> ceiling:4204
> settle:3721
> time:1649726910500
> }
>
> {
> price:"15405.000000000004"
> floor:13625
> ceiling:17340
> settle:15485
> time:1649728303500
> }
>
> {
> price:"800.4000000000001"
> floor:720.6
> ceiling:881
> settle:"800.8000000000001"
> time:1649728048000
> }
>
> Note quotations above. I wonder how encoding/json can made transformation
> from a float64 inside struct Fvprices  to a string instead? It seems that
> only long decimals would trigger such an error while short decimals won't:
>
> {
> price:2910
> floor:2443.5
> ceiling:3305.5
> settle:2874.5
> time:1649728261026
> }
>
> How could that happen? I am really puzzled.
>
> Regards,
>     Zhaoxun
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/f42b29b3-de17-48c6-9f71-1176f1288396n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/f42b29b3-de17-48c6-9f71-1176f1288396n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAHEMsqb24d5B4NxzeaCNdR%2B_%3DQgxbyGJSsSgjS8jyQa%3DQFHmgg%40mail.gmail.com.

Reply via email to