防止Go中的重复功能仅限现场结构

Flo*_*ish 3 code-duplication go

编辑:我已更新下面的代码示例,以更好地说明问题.

假设我有两个不需要任何功能的仅字段结构.

假设它们代表来自数据库的两种类似的数据:

type Boy struct {
    Name          string
    FavoriteColor string
    BirthDay      time.Time
}

type Girl struct {
    Name           string
    FavoriteFlower string
    BirthDay       time.Time
}
Run Code Online (Sandbox Code Playgroud)

我为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
}
Run Code Online (Sandbox Code Playgroud)

现在,由于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
}
Run Code Online (Sandbox Code Playgroud)

有没有办法避免上述简单结构的代码重复?我不想为我想要实现它的每个新结构复制我的函数.

接口在这里不是一个选项,因为两个结构都没有任何功能可以说(并且为了满足接口而添加虚拟功能听起来像是向后解决方案).

编辑:在考虑我接受的解决方案后,我现在相信接口也是解决此问题的有效方法.感谢@ThunderCat提出来!

Ada*_*ith 5

正如ThunderCat在评论中提到的:将公共代码拉入单独的函数并调用.

func CheckBirthday(date, birthdate time.Time, name, gift string) (numDays int) {
    if date.Before(birthdate) {
        // Greet how many days before birthday
        numDays = int(birthdate.Sub(date).Hours() / 24)
        fmt.Printf("Hi, %s! Only %d days until your birthday! I hear your favorite is %s!\n", name, numDays, gift)
    } else if date.Equal(birthdate) {
        // Greet happy birthday
        fmt.Printf("Happy birthday, %s! I brought you a %s as a present!\n", name, gift)
    } else {
        // Greet belated birthday
        numDays = int(date.Sub(girl.birthday).Hours() / 24)
        fmt.Printf("Sorry I'm %d days late, %s! Here is a %s to cheer you up!\n", numDays, name, gift)
    }

    return
}

func CheckBirthdayBoy(date time.Time, boy Boy) int {
    return CheckBirthday(date, boy.BirthDay, boy.Name, boy.FavoriteColor)
}

func CheckBirthdayGirl(date time.Time, girl Girl) int {
    return CheckBirthday(date, girl.BirthDay, girl.Name, girl.FavoriteFlower)
}
Run Code Online (Sandbox Code Playgroud)