go-redis/redis · error
redis: unexpected type=%T for Uint64
Error message
redis: unexpected type=%T for Uint64
What it means
Returned by toUint64 (command.go:700) when Cmd.Uint64() is called on a reply value that is neither int64 nor string. int64 is cast to uint64; string is parsed via strconv.ParseUint; any other type (array/nil/etc.) yields this error. Common when reading unsigned counters (LPOS, SLOWLOG, some module replies).
Source
Thrown at command.go:707
}
}
func (cmd *Cmd) Uint64() (uint64, error) {
cmd.await()
if cmd.err != nil {
return 0, cmd.err
}
return toUint64(cmd.val)
}
func toUint64(val interface{}) (uint64, error) {
switch val := val.(type) {
case int64:
return uint64(val), nil
case string:
return strconv.ParseUint(val, 10, 64)
default:
err := fmt.Errorf("redis: unexpected type=%T for Uint64", val)
return 0, err
}
}
func (cmd *Cmd) Float32() (float32, error) {
cmd.await()
if cmd.err != nil {
return 0, cmd.err
}
return toFloat32(cmd.val)
}
func toFloat32(val interface{}) (float32, error) {
switch val := val.(type) {
case int64:
return float32(val), nil
case string:
f, err := strconv.ParseFloat(val, 32)View on GitHub (pinned to 36d97525cd)
Solutions
- Use the typed command whose reply parser stores int64 directly (e.g. IntCmd then cast, or a dedicated Uint64Cmd if provided).
- Type-switch on cmd.Result() when using Do.
- Confirm cmd.Err() (including redis.Nil) before reading the value.
Example fix
// before u, err := client.Do(ctx, "STRLEN", "k").Uint64() // STRLEN is integer → ok, but only for int-shaped replies // after n, err := client.StrLen(ctx, "k").Result() // Int64 var u uint64 = uint64(n)
Defensive patterns
Strategy: type-guard
Type guard
func asUint64(cmd *redis.Cmd) (uint64, error) {
if err := cmd.Err(); err != nil { return 0, err }
switch v := cmd.Val().(type) {
case int64:
if v < 0 { return 0, fmt.Errorf("negative: %d", v) }
return uint64(v), nil
case string:
return strconv.ParseUint(v, 10, 64)
default:
return 0, fmt.Errorf("not a uint64: %T", v)
}
} Try / catch
u, err := cmd.Uint64()
if err != nil && strings.Contains(err.Error(), "unexpected type") {
// reply was not a numeric scalar — switch accessor
} Prevention
- Use a typed integer command then cast to uint64 when negative is impossible.
- Type-switch on Result() for raw Do replies.
- Guard against negative int64 before casting to uint64.
When it happens
Trigger: Calling Uint64() on a raw Cmd holding an array or non-numeric reply; using Uint64() on a reply that is actually a bulk status string; calling Uint64() when the underlying value is nil.
Common situations: Do-based generic Cmd mis-typed as unsigned; reading a counter from a command that wraps the value inside a sub-array; mismatched accessor vs. command.
Related errors
- redis: unexpected type=%T for String
- redis: unexpected type=%T for Int
- redis: unexpected type=%T for Int64
- redis: unexpected type=%T for Float32
- redis: unexpected type=%T for Float64
AI-assisted analysis of go-redis/redis@36d97525cd (2026-08-06).
Data as JSON: /data/errors/290c528295fb2b4c.json.
Report an issue: GitHub.