
Go 不支持类似 C++ 的引用传参,必须显式传递接口变量的指针(如 &i),再通过反射或类型断言解引用赋值,才能让调用方的接口变量非 nil 并持有具体实现。
go 不支持类似 c++ 的引用传参,必须显式传递接口变量的指针(如 `&i`),再通过反射或类型断言解引用赋值,才能让调用方的接口变量非 nil 并持有具体实现。
在 Go 中,所有参数都是值传递——包括接口类型。接口本身是一个包含类型信息和数据指针的两字宽结构体(interface{} 占 16 字节)。当你写 Get("title", i) 时,传递的是该接口值的完整副本;函数内部对 iRef 的重新赋值(如 iRef = new(AType...))仅修改局部副本,对调用方的 i 完全无影响。因此,i 在调用后仍为 nil 是符合语言规范的预期行为。
要实现“由函数填充接口变量”的效果,核心思路是:*传递接口变量的地址,即 `interface{}` 类型指针,并在函数内对其解引用后赋值**。注意:这不是传递“指向底层实现的指针”,而是传递“指向接口变量本身的指针”。
✅ 正确做法:使用 *interface{} + 类型断言
type CustomInterface interface {
SomeOperationWithoutTypeAssertion()
}
type ATypeWhichImplementsTheUnderlyingInterface struct{}
func (a ATypeWhichImplementsTheUnderlyingInterface) SomeOperationWithoutTypeAssertion() {
fmt.Println("Operation executed!")
}
func Get(title string, out *interface{}) {
// 创建具体实现实例
impl := ATypeWhichImplementsTheUnderlyingInterface{}
// 解引用 *interface{},将 impl 赋值给调用方的接口变量
*out = impl
}
// 使用示例
func main() {
var i CustomInterface
fmt.Printf("Before: %+v (nil? %t)\n", i, i == nil) // Before: <nil> (nil? true)
Get("title", (*interface{})(unsafe.Pointer(&i))) // ⚠️ 不推荐:需 unsafe,且易出错
// 更安全、更清晰的写法:显式转换为 *interface{}
// 注意:i 是 CustomInterface 类型,其底层是 interface{},可取地址
Get("title", &i) // ✅ 编译通过 —— 因为 CustomInterface 满足 interface{},&i 是 *CustomInterface,
// 但 Get 参数是 *interface{},类型不匹配!需调整函数签名。
}⚠️ 上述 &i 直接传入 *interface{} 会编译失败:*CustomInterface ≠ *interface{}。因此,*推荐统一使用 `interface{}` 作为输出参数类型,并在调用时显式取地址**:
func Get(title string, out *interface{}) {
*out = ATypeWhichImplementsTheUnderlyingInterface{}
}
func main() {
var i interface{} // 使用 interface{} 作为通用接收容器
fmt.Printf("Before: %+v (nil? %t)\n", i, i == nil)
Get("title", &i)
// 类型断言为 CustomInterface(若需调用接口方法)
if ci, ok := i.(CustomInterface); ok {
ci.SomeOperationWithoutTypeAssertion() // 输出: Operation executed!
}
}? 替代方案:返回接口值(更 Go-idiomatic)
Go 社区普遍认为,显式返回值比“通过指针填充”更清晰、更安全:
func Get(title string) CustomInterface {
return ATypeWhichImplementsTheUnderlyingInterface{}
}
// 使用
i := Get("title")
i.SomeOperationWithoutTypeAssertion()此方式避免了类型转换、panic 风险与内存模型混淆,也完全符合 Go 的简洁哲学。
⚠️ 注意事项与常见误区
- 不要滥用 reflect 实现通用填充:虽然可用 reflect.ValueOf(out).Elem().Set(reflect.ValueOf(impl)),但反射性能低、可读性差,且无法静态检查类型安全性;
- *`interface{}不等于T`:它是指向接口变量的指针,不是指向具体类型的二级指针;
- nil 接口 ≠ nil 底层值:一个接口为 nil,当且仅当其动态类型和动态值均为 nil;而 *interface{} 为 nil 时,解引用会 panic;
- 始终校验类型断言结果:使用 value, ok := x.(T) 而非 x.(T),防止运行时 panic。
✅ 总结
Go 中无法“隐式引用传参”。若需函数填充调用方的接口变量,请:
- 将函数参数声明为 *interface{};
- 调用时传入 &i(其中 i 类型为 interface{} 或可赋值给 interface{} 的接口);
- 函数内通过 *out = value 赋值;
- (更推荐)改用返回值方式,提升代码可读性与健壮性。
真正的 Go 风格,是拥抱值语义,而非模拟其他语言的引用机制。










