{"id":"23cdcfa209e41eec","repo":"go-redis/redis","slug":"redis-unexpected-type-t-for-int64","errorCode":null,"errorMessage":"redis: unexpected type=%T for Int64","messagePattern":"redis: unexpected type=%T for Int64","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command.go","lineNumber":687,"sourceCode":"\t}\n}\n\nfunc (cmd *Cmd) Int64() (int64, error) {\n\tcmd.await()\n\tif cmd.err != nil {\n\t\treturn 0, cmd.err\n\t}\n\treturn toInt64(cmd.val)\n}\n\nfunc toInt64(val interface{}) (int64, error) {\n\tswitch val := val.(type) {\n\tcase int64:\n\t\treturn val, nil\n\tcase string:\n\t\treturn strconv.ParseInt(val, 10, 64)\n\tdefault:\n\t\terr := fmt.Errorf(\"redis: unexpected type=%T for Int64\", val)\n\t\treturn 0, err\n\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)","sourceCodeStart":669,"sourceCodeEnd":705,"githubUrl":"https://github.com/go-redis/redis/blob/36d97525cd8076aed67cddf54778e9ea84550929/command.go#L669-L705","documentation":"Returned by toInt64 (command.go:680) when Cmd.Int64() is called on a reply value that is neither int64 nor string. int64 (integer reply) is returned directly; string is parsed via strconv.ParseInt; any other concrete type (array, nil, map) falls through to this error.","triggerScenarios":"Calling Int64() on a Cmd holding an array reply (e.g. raw Do of a command that returns a list) or a non-numeric bulk; calling Int64() on a Float-typed reply that arrived as something other than string/int64.","commonSituations":"Generic Cmd from Do mis-typed; wrong accessor for the command; expecting an integer from a command that embeds the count inside an array.","solutions":["Use the typed command method that matches the reply (IntCmd, Int64SliceCmd, etc.).","Type-switch on cmd.Result() before converting when using Do.","Handle redis.Nil and other errors before reading val."],"exampleFix":"// before\nv, err := client.Do(ctx, \"GET\", \"counter\").Int64() // GET returns bulk-string; works only if numeric, fails for non-numeric\n\n// after — prefer typed Get + parse defensively\nv, err := client.Get(ctx, \"counter\").Int64() // StringCmd.Int64 parses the bulk string\nif err != nil { return 0, err }","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func asInt64(cmd *redis.Cmd) (int64, error) {\n    if err := cmd.Err(); err != nil { return 0, err }\n    switch v := cmd.Val().(type) {\n    case int64:\n        return v, nil\n    case string:\n        return strconv.ParseInt(v, 10, 64)\n    default:\n        return 0, fmt.Errorf(\"not an int64: %T\", v)\n    }\n}","tryCatchPattern":"n, err := cmd.Int64()\nif err != nil && strings.Contains(err.Error(), \"unexpected type\") {\n    // switch to a typed command or type-switch on Result()\n}","preventionTips":["Use the typed command whose parser stores int64 directly.","Type-switch on Result() for raw Do replies.","Handle redis.Nil before coercing."],"tags":["command","type-conversion","reply-parsing","api-misuse"],"analyzedSha":"36d97525cd8076aed67cddf54778e9ea84550929","analyzedAt":"2026-08-06T01:08:27.376Z","schemaVersion":2}