go compile_error ai_generated true

返回值过多 有 (T, error) 想要 (T)

too many return values have (T, error) want (T)

ID: go/too-many-return-values

其他格式: JSON · Markdown 中文 · English
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.

generic

官方文档

https://go.dev/ref/spec#Function_types

解决方案

  1. Assign both return values to variables and handle the error: val, err := someFunc(); if err != nil { ... }
  2. If the function should return only one value, wrap it in a helper: func wrapper() T { val, _ := someFunc(); return val }

无效尝试

常见但无效的做法:

  1. 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.

  2. Changing the function signature to remove the error return 95% 失败

    Modifying the library function breaks its contract and may cause other compilation errors.

  3. Using a type assertion on the result 98% 失败

    Type assertion cannot reduce the number of return values; it only works on interfaces.