{"id":"290c528295fb2b4c","repo":"go-redis/redis","slug":"redis-unexpected-type-t-for-uint64","errorCode":null,"errorMessage":"redis: unexpected type=%T for Uint64","messagePattern":"redis: unexpected type=%T for Uint64","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command.go","lineNumber":707,"sourceCode":"\t}\n}\n\nfunc (cmd *Cmd) Uint64() (uint64, error) {\n\tcmd.await()\n\tif cmd.err != nil {\n\t\treturn 0, cmd.err\n\t}\n\treturn toUint64(cmd.val)\n}\n\nfunc toUint64(val interface{}) (uint64, error) {\n\tswitch val := val.(type) {\n\tcase int64:\n\t\treturn uint64(val), nil\n\tcase string:\n\t\treturn strconv.ParseUint(val, 10, 64)\n\tdefault:\n\t\terr := fmt.Errorf(\"redis: unexpected type=%T for Uint64\", val)\n\t\treturn 0, err\n\t}\n}\n\nfunc (cmd *Cmd) Float32() (float32, error) {\n\tcmd.await()\n\tif cmd.err != nil {\n\t\treturn 0, cmd.err\n\t}\n\treturn toFloat32(cmd.val)\n}\n\nfunc toFloat32(val interface{}) (float32, error) {\n\tswitch val := val.(type) {\n\tcase int64:\n\t\treturn float32(val), nil\n\tcase string:\n\t\tf, err := strconv.ParseFloat(val, 32)","sourceCodeStart":689,"sourceCodeEnd":725,"githubUrl":"https://github.com/go-redis/redis/blob/36d97525cd8076aed67cddf54778e9ea84550929/command.go#L689-L725","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nu, err := client.Do(ctx, \"STRLEN\", \"k\").Uint64() // STRLEN is integer → ok, but only for int-shaped replies\n\n// after\nn, err := client.StrLen(ctx, \"k\").Result() // Int64\nvar u uint64 = uint64(n)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func asUint64(cmd *redis.Cmd) (uint64, error) {\n    if err := cmd.Err(); err != nil { return 0, err }\n    switch v := cmd.Val().(type) {\n    case int64:\n        if v < 0 { return 0, fmt.Errorf(\"negative: %d\", v) }\n        return uint64(v), nil\n    case string:\n        return strconv.ParseUint(v, 10, 64)\n    default:\n        return 0, fmt.Errorf(\"not a uint64: %T\", v)\n    }\n}","tryCatchPattern":"u, err := cmd.Uint64()\nif err != nil && strings.Contains(err.Error(), \"unexpected type\") {\n    // reply was not a numeric scalar — switch accessor\n}","preventionTips":["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."],"tags":["command","type-conversion","reply-parsing","api-misuse"],"analyzedSha":"36d97525cd8076aed67cddf54778e9ea84550929","analyzedAt":"2026-08-06T01:08:27.376Z","schemaVersion":2}