go
type_error
ai_generated
true
不能将类型 T 的值用作参数中的类型 *T
cannot use (variable of type T) as type *T in argument
ID: go/cannot-use-type-without-star
92%修复率
88%置信度
1证据数
2023-11-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| go1.21 | active | — | — | — |
| go1.22 | active | — | — | — |
| go1.23 | active | — | — | — |
根因分析
函数期望接收指向类型的指针 (*T),但传入了非指针值 (T),导致类型不匹配。
English
A function expects a pointer to a type (*T) but receives a non-pointer value (T), causing a type mismatch.
官方文档
https://go.dev/ref/spec#Address_operators解决方案
-
Pass the address of the variable: `functionName(&variable)` instead of `functionName(variable)`.
-
If the variable is not addressable (e.g., map value), copy it first: `tmp := variable; functionName(&tmp)`.
无效尝试
常见但无效的做法:
-
70% 失败
Casting requires the value to be addressable; this often leads to another compilation error.
-
60% 失败
Modifying library or external function signatures is not always possible or breaks other code.