Skip to content
The Chat Archive
gpt-4-0314· OpenAI WildChat-4.8M (AI2)

Why does the following go code fail when using go-redis v9 but not v8? func (s…

  1. 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()

  2. Assistant

    The go-redis library changed the behavior of the Float64() method between versions 8 and 9 in how it handles the redis.Nil error when the Redis key does not exist.

    In version 8, when calling Float64() on a redis.StringCmd that represents a non-existing key, it would return a redis.Nil error directly. In version 9, the library first tries to parse the empty string and return the strconv.ParseFloat: parsing "": invalid syntax error.

    To handle this change in behavior, you can modify your parseTotalResult and parseDailyResult functions to first check whether the error is redis.Nil before calling Float64(). 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 the redis.StringCmd. Then, they check if the error is redis.Nil and handle it accordingly. If the error is not redis.Nil, it attempts to parse the string using strconv.ParseFloat and proceeds with your original logic.