go
compile_error
ai_generated
true
too many return values have (T, error) want (T)
ID: go/too-many-return-values
90%Fix Rate
88%Confidence
1Evidence
2023-06-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| go1.20 | active | — | — | — |
| go1.21 | active | — | — | — |
| go1.22 | active | — | — | — |
Root Cause
A function expecting a single return value is being assigned or used where a multi-return function is called, often due to mismatched function signatures or missing error handling.
generic中文
期望单个返回值的函数被赋值或用于调用多返回值函数的位置,通常是由于函数签名不匹配或缺少错误处理。
Official Documentation
https://go.dev/ref/spec#Function_typesWorkarounds
-
95% success Assign both return values to variables and handle the error: val, err := someFunc(); if err != nil { ... }
Assign both return values to variables and handle the error: val, err := someFunc(); if err != nil { ... } -
85% success If the function should return only one value, wrap it in a helper: func wrapper() T { val, _ := someFunc(); return val }
If the function should return only one value, wrap it in a helper: func wrapper() T { val, _ := someFunc(); return val }
中文步骤
Assign both return values to variables and handle the error: val, err := someFunc(); if err != nil { ... }If the function should return only one value, wrap it in a helper: func wrapper() T { val, _ := someFunc(); return val }
Dead Ends
Common approaches that don't work:
-
Adding an underscore to ignore the error: val, _ := someFunc()
90% fail
This only works if the caller expects two values; if the caller expects one, the compile error persists.
-
Changing the function signature to remove the error return
95% fail
Modifying the library function breaks its contract and may cause other compilation errors.
-
Using a type assertion on the result
98% fail
Type assertion cannot reduce the number of return values; it only works on interfaces.