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

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

generic

官方文档

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

解决方案

  1. Pass the address of the variable: `functionName(&variable)` instead of `functionName(variable)`.
  2. If the variable is not addressable (e.g., map value), copy it first: `tmp := variable; functionName(&tmp)`.

无效尝试

常见但无效的做法:

  1. 70% 失败

    Casting requires the value to be addressable; this often leads to another compilation error.

  2. 60% 失败

    Modifying library or external function signatures is not always possible or breaks other code.