{"record":{"id":"296b93b58e70fef1","repo":"go-delve/delve","slug":"can-not-convert-constant-s-to-int","errorCode":null,"errorMessage":"can not convert constant %s to int","messagePattern":"can not convert constant (.+?) to int","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/eval.go","lineNumber":2723,"sourceCode":"func equalChildren(xv, yv *Variable, shortcircuit bool) (bool, error) {\n\tr := true\n\tfor i := range xv.Children {\n\t\teql, err := compareOp(token.EQL, &xv.Children[i], &yv.Children[i])\n\t\tif err != nil {\n\t\t\treturn false, err\n\t\t}\n\t\tr = r && eql\n\t\tif !r && shortcircuit {\n\t\t\treturn false, nil\n\t\t}\n\t}\n\treturn r, nil\n}\n\nfunc (v *Variable) asInt() (int64, error) {\n\tif v.DwarfType == nil {\n\t\tif v.Value.Kind() != constant.Int {\n\t\t\treturn 0, fmt.Errorf(\"can not convert constant %s to int\", v.Value)\n\t\t}\n\t} else {\n\t\tv.loadValue(loadSingleValue)\n\t\tif v.Unreadable != nil {\n\t\t\treturn 0, v.Unreadable\n\t\t}\n\t\tif _, ok := v.DwarfType.(*godwarf.IntType); !ok {\n\t\t\treturn 0, fmt.Errorf(\"can not convert value of type %s to int\", v.DwarfType.String())\n\t\t}\n\t}\n\tn, _ := constant.Int64Val(v.Value)\n\treturn n, nil\n}\n\nfunc (v *Variable) asUint() (uint64, error) {\n\tif v.DwarfType == nil {\n\t\tif v.Value.Kind() != constant.Int {\n\t\t\treturn 0, fmt.Errorf(\"can not convert constant %s to uint\", v.Value)","sourceCodeStart":2705,"sourceCodeEnd":2741,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/eval.go#L2705-L2741","documentation":"Variable.asInt converts a variable to an int64 for uses like array indexing or shift counts. If the variable has no DWARF type, it is treated as an untyped constant, and only constant.Int values can be converted; a float or string constant reaching asInt produces this error.","triggerScenarios":"Using a non-integer constant where an integer is required: `p arr[1.5]`, `p arr[\"key\"]`, or a shift count given as an untyped float constant, in expressions evaluated by EvalScope.","commonSituations":"Typing an index with a decimal point out of habit (`a[2.0]`), or using a string where a numeric index is expected while probing arrays/slices in the debugger.","solutions":["Use an integer literal or cast: `p arr[int(2.0)]` or `p arr[2]`.","Convert floats explicitly with int(): `p arr[int(f)]`.","Check the constant for a stray decimal point or wrong value."],"exampleFix":"// before\np arr[2.0]\n// after\np arr[2]","handlingStrategy":"validation","validationCode":"// indexes and shift counts must be integer constants\n// write arr[2], not arr[2.0]; cast floats explicitly: arr[int(f)]","typeGuard":"func isIntConstant(v *proc.Variable) bool {\n    if v == nil {\n        return false\n    }\n    if v.DwarfType == nil {\n        return v.Value != nil && v.Value.Kind() == constant.Int\n    }\n    switch v.Kind {\n    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,\n        reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:\n        return true\n    }\n    return false\n}","tryCatchPattern":null,"preventionTips":["Write array/slice indexes as plain integer literals.","Cast floats with int() before using them as indexes.","Avoid decimal points in indexes — 2.0 is still a float constant."],"tags":["debugger","expression-evaluation","type-conversion"],"backgroundTag":"invalid-constant-conversion","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}