Why does the following go code fail when using go-redis v9 but not v8? func (s…
- User
Why does the following go code fail when using go-redis v9 but not v8?
func (s *RedisStorage) getResults(ctx context.Context, resultID string) error { pipeline := s.client.Pipeline()
totalResult := pipeline.Get(ctx, "key1") dailyResult := pipeline.Get(ctx, "key2") allResult := pipeline.HGetAll(ctx, "key3") if _, err := pipeline.Exec(ctx); err != nil && !errors.Is(err, redis.Nil) { return fmt.Errorf("failed getting results for b %s: error executing pipeline: %w", resultID, err) } total, err := parseTotalResult(totalResult) if err != nil { return err } daily, err := parseDailyResult(dailyResult) if err != nil { return err } return nil}
func parseTotalResult(result *redis.StringCmd) (float64, error) { x, err := result.Float64() if err != nil && !errors.Is(err, redis.Nil) { return 0, fmt.Errorf("failed converting result to Float64: %w", err) }
return x, nil}
func parseDailyResult(result *redis.StringCmd) (float64, error) { x, err := result.Float64() if err != nil && !errors.Is(err, redis.Nil) { return 0, fmt.Errorf("failed converting daily result to Float64: %w", err) }
return x, nil}
Both, "key1" and "key2" do not exist in Redis. The error is "strconv.ParseFloat: parsing "": invalid syntax" inside the parseDailyResult function when running x, err := result.Float64()
- Assistant
The go-redis library changed the behavior of the
Float64()method between versions 8 and 9 in how it handles theredis.Nilerror when the Redis key does not exist.In version 8, when calling
Float64()on aredis.StringCmdthat represents a non-existing key, it would return aredis.Nilerror directly. In version 9, the library first tries to parse the empty string and return thestrconv.ParseFloat: parsing "": invalid syntaxerror.To handle this change in behavior, you can modify your
parseTotalResultandparseDailyResultfunctions to first check whether the error isredis.Nilbefore callingFloat64(). Here's how you could modify both functions:func parseTotalResult(result *redis.StringCmd) (float64, error) { str, err := result.Result() // Changed to Result() to get the raw string if errors.Is(err, redis.Nil) { return 0, nil } else if err != nil { return 0, fmt.Errorf("failed getting result string: %w", err) } x, err := strconv.ParseFloat(str, 64) // Use strconv.ParseFloat, since we already have the string if err != nil { return 0, fmt.Errorf("failed converting result to Float64: %w", err) } return x, nil } func parseDailyResult(result *redis.StringCmd) (float64, error) { str, err := result.Result() // Changed to Result() to get the raw string if errors.Is(err, redis.Nil) { return 0, nil } else if err != nil { return 0, fmt.Errorf("failed getting daily result string: %w", err) } x, err := strconv.ParseFloat(str, 64) // Use strconv.ParseFloat, since we already have the string if err != nil { return 0, fmt.Errorf("failed converting daily result to Float64: %w", err) } return x, nil }In this modified version, both functions first call
Result()to get the actual string value of theredis.StringCmd. Then, they check if the error isredis.Niland handle it accordingly. If the error is notredis.Nil, it attempts to parse the string usingstrconv.ParseFloatand proceeds with your original logic.