我一直在阅读go-lang 界面文档;但是我仍然不清楚是否有可能实现我想要的
type A struct {
ID SomeSpecialType
}
type B struct {
ID SomeSpecialType
}
func (a A) IDHexString() string {
return a.ID.Hex()
}
func (b B) IDHexString() string {
return b.ID.Hex()
}
这将正常工作;但是我更喜欢一些惯用的方法来将通用方法应用于两种类型并且只定义一次。就像是:
type A struct {
ID SomeSpecialType
}
type B struct {
ID SomeSpecialType
}
func (SPECIFY_TYPE_A_AND_B_HERE) IDHexString() string {
return A_or_B.ID.Hex()
}