go
compile_error
ai_generated
true
返回值过多 有 (T, error) 想要 (T)
too many return values have (T, error) want (T)
ID: go/too-many-return-values
90%修复率
88%置信度
1证据数
2023-06-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| go1.20 | active | — | — | — |
| go1.21 | active | — | — | — |
| go1.22 | active | — | — | — |
根因分析
期望单个返回值的函数被赋值或用于调用多返回值函数的位置,通常是由于函数签名不匹配或缺少错误处理。
English
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.
官方文档
https://go.dev/ref/spec#Function_types解决方案
-
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 }
无效尝试
常见但无效的做法:
-
Adding an underscore to ignore the error: val, _ := someFunc()
90% 失败
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% 失败
Modifying the library function breaks its contract and may cause other compilation errors.
-
Using a type assertion on the result
98% 失败
Type assertion cannot reduce the number of return values; it only works on interfaces.