aphelios.io

接收者类型_指针_值
Created on 13 Oct 2026 at 00:00 golang

值接收者 (r Runtime) 方法同时绑定:Runtime 值类型 和 *Runtime 指针类型。

 
 type Runtime int
 func (r Runtime) MarshalJSON() {}

var a Runtime var b *Runtime = &a // a.MarshalJSON() ✅ 可以使用 // b.MarshalJSON() ✅ 可以(自动解引用)

指针接收者 (r *Runtime) 方法只绑定 *Runtime 指针类型,值类型 Runtime 不会拥有这个方法


 type Runtime int
 func (r *Runtime) MarshalJSON() {}

var a Runtime var b *Runtime = &a // a.MarshalJSON() ❌ a是值,没有这个方法! // b.MarshalJSON() ✅ 指针可以调用