【发布时间】:2018-07-10 05:36:07
【问题描述】:
编辑:我更新了以下代码示例以更好地说明问题。
假设我有 2 个不需要任何功能的仅字段结构。
假设它们代表数据库中的 2 类相似数据:
type Boy struct {
Name string
FavoriteColor string
BirthDay time.Time
}
type Girl struct {
Name string
FavoriteFlower string
BirthDay time.Time
}
我为 Boy 结构体编写了一个函数,它根据给定的日期和男孩的信息打印问候语。
假设这是一个更复杂的函数的占位符,该函数基于time.Time 字段执行某些操作,并返回将在应用程序的其他地方使用的int:
func CheckBirthDayBoy(date time.Time, boy Boy) int {
numDays := 0
if date.Before(boy.BirthDay) {
// Greet how many days before birthday
numDays = int(boy.BirthDay.Sub(date).Hours() / 24)
fmt.Println("Hi, " + boy.Name + "! Only " + strconv.Itoa(numDays) + " days until your birthday! I hear your favorite color is " + boy.FavoriteColor + "!")
} else if date.Equal(boy.BirthDay) {
// Greet happy birthday
fmt.Println("Happy birthday, " + boy.Name + "! I brought you something " + boy.FavoriteColor + " as a present!")
} else {
// Greet belated birthday
numDays = int(date.Sub(boy.BirthDay).Hours() / 24)
fmt.Println("Sorry I'm " + strconv.Itoa(numDays) + " days late, " + boy.Name + "! Here is something " + boy.FavoriteColor + " to cheer you up!")
}
return numDays
}
现在,由于 Go 是一种强类型语言,并且没有泛型,我最终不得不为 Girl 结构体编写一个重复的函数:
func CheckBirthDayGirl(date time.Time, girl Girl) int {
numDays := 0
if date.Before(girl.BirthDay) {
// Greet how many days before birthday
numDays = int(girl.BirthDay.Sub(date).Hours() / 24)
fmt.Println("Hi, " + girl.Name + "! Only " + strconv.Itoa(numDays) + " days until your birthday! I hear your favorite flower is a " + girl.FavoriteFlower + "!")
} else if date.Equal(girl.BirthDay) {
// Greet happy birthday
fmt.Println("Happy birthday, " + girl.Name + "! I brought you a " + girl.FavoriteFlower + " as a present!")
} else {
// Greet belated birthday
numDays = int(date.Sub(girl.BirthDay).Hours() / 24)
fmt.Println("Sorry I'm " + strconv.Itoa(numDays) + " days late, " + girl.Name + "! Here is a " + girl.FavoriteFlower + " to cheer you up!")
}
return numDays
}
有没有办法避免上述简单结构的代码重复?我不想为每个要实现它的新结构复制我的函数。
接口在这里不是一个选项,因为这两个结构都没有任何功能可言(并且为了满足接口而添加虚拟功能对我来说听起来像是一种倒退的解决方案)。
编辑:在考虑了我接受的解决方案之后,我现在相信接口也是解决这个问题的有效解决方案。感谢@ThunderCat 提出!
【问题讨论】:
-
为什么不直接将
time.Time传递给CheckBirthday 函数? -
@Hau Ma 上面的代码已经简化以说明问题。这也可能发生在更复杂的代码中。
-
如何使用一个人而不是性别代码。更有意义。
-
@Derek 嗯...我可能需要修改我的代码示例以更好地说明问题。对困惑感到抱歉。将在几分钟内更新示例。
-
是的,我建议提供一个尽可能接近问题的示例
标签: go code-duplication