Files
pro-go-study/27-reflection/reflection/interfaces.go
yanzuoguang e278aa30a7 feat(reflect): 增强反射功能并重构方法调用逻辑
- 新增 mapSlice1 函数支持通过反射映射切片元素
- 实现 makeMapperFunc 函数动态创建映射函数- 添加 inspectMethods 和 executeFirstVoidMethod 等方法用于检查和执行结构体方法
-重构 main29 和 main30 示例以展示新增的反射能力
- 修改 Purchase 结构体字段顺序以优化内存布局
-重命名 Purchase 的 calcTotal 方法为 GetTotal 提高一致性
- 将 Customer.GetName 方法移至正确位置避免重复定义
2025-11-01 22:41:07 +08:00

33 lines
463 B
Go

package main
import "fmt"
type NamedItem interface {
GetName() string
unexportedMethod()
}
type CurrencyItem interface {
GetAmount() string
currencyName() string
}
func (p *Product) GetName() string {
return p.Name
}
func (c *Customer) GetName() string {
return c.Name
}
func (p *Product) GetAmount() string {
return fmt.Sprintf("$%.2f", p.Price)
}
func (p *Product) currencyName() string {
return "USD"
}
func (p *Product) unexportedMethod() {
}