首頁  >  文章  >  後端開發  >  如何使用介面使 Go 函數通用:類型約束解決方案

如何使用介面使 Go 函數通用:類型約束解決方案

DDD
DDD原創
2024-10-27 02:28:30342瀏覽

 How to Make Go Functions Generic Using Interfaces: A  Type Constraint Solution

Go 中泛型方法參數:解決類型約束問題

問題:

考慮以下Go 程式碼:

<code class="go">package main

import (
    "fmt"
    "strconv"
)

type Mammal struct {
    ID int
    Name string
}

type Human struct {
    ID int
    Name string
    HairColor string
}

func Count(ms []Mammal) *[]string {
    IDs := make([]string, len(ms))
    for i, m := range ms {
        IDs[i] = strconv.Itoa(int(m.ID))
    }
    return &IDs
}

func main() {
    ... // Code to create Mammal and Human slices
    numberOfMammalIDs := Count(mammals)
    numberOfHumanIDs := Count(humans)
    fmt.Println(numberOfMammalIDs)
    fmt.Println(numberOfHumanIDs)
}</code>

此程式碼無法編譯,並出現錯誤error prog.go:39: Cannot use humans (type []Human) as type []Mammal in argument to Count。出現此問題的原因是 Count 函數需要一個 Mammal 結構數組,但我們傳遞的是一個 Human 結構數組。我們如何解決這個類型約束並使 Count 函數足夠通用以接受任何具有 ID 屬性的類型?

解決方案:

1.使用介面:

以定義 ID 屬性的介面取代特定類型。例如:

<code class="go">type Mammal interface {
    GetID() int
}

type Human interface {
    Mammal
    GetHairColor() string
}</code>

2。嵌入介面:

為了避免在哺乳動物和人類介面中重複ID 方法,請使用嵌入介面:

<code class="go">type MammalImpl struct {
    ID int
    Name string
}

func (m MammalImpl) GetID() int {
    return m.ID
}

type HumanImpl struct {
    MammalImpl
    HairColor string
}</code>

3.更新計數函數:

修改計數函數以使用Mammal 介面而不是特定的Mammal 類型:

<code class="go">func Count(ms []Mammal) *[]string {
    IDs := make([]string, len(ms))
    for i, m := range ms {
        IDs[i] = strconv.Itoa(m.GetID())
    }
    return &IDs
}</code>

4.建立符合介面的切片:

4.建立符合介面的切片:

<code class="go">mammals := []Mammal{
    MammalImpl{1, "Carnivorious"},
    MammalImpl{2, "Ominivorious"},
}

humans := []Mammal{
    HumanImpl{MammalImpl: MammalImpl{ID: 1, Name: "Peter"}, HairColor: "Black"},
    HumanImpl{MammalImpl: MammalImpl{ID: 2, Name: "Paul"}, HairColor: "Red"},
}</code>

建立實現哺乳動物介面的切片:

5.用法範例:

<code class="go">package main

import (
    "fmt"
    "strconv"
)

type Mammal interface {
    GetID() int
}

type Human interface {
    Mammal
    GetHairColor() string
}

type MammalImpl struct {
    ID   int
    Name string
}

func (m MammalImpl) GetID() int {
    return m.ID
}

type HumanImpl struct {
    MammalImpl
    HairColor string
}

func (h HumanImpl) GetHairColor() string {
    return h.HairColor
}

func Count(ms []Mammal) *[]string {
    IDs := make([]string, len(ms))
    for i, m := range ms {
        IDs[i] = strconv.Itoa(m.GetID())
    }
    return &IDs
}

func main() {
    mammals := []Mammal{
        MammalImpl{1, "Carnivorious"},
        MammalImpl{2, "Ominivorous"},
    }

    humans := []Mammal{
        HumanImpl{MammalImpl: MammalImpl{ID: 1, Name: "Peter"}, HairColor: "Black"},
        HumanImpl{MammalImpl: MammalImpl{ID: 2, Name: "Paul"}, HairColor: "Red"},
    }

    numberOfMammalIDs := Count(mammals)
    numberOfHumanIDs := Count(humans)
    fmt.Println(numberOfMammalIDs) // [1 2]
    fmt.Println(numberOfHumanIDs) // [1 2]
}</code>

現已成功編譯的用法範例:

現已成功編譯的用法範例:透過使用接口和嵌入式接口,我們使Count 函數足夠通用,可以處理任何實現哺乳動物接口,有效解決類型約束問題。

以上是如何使用介面使 Go 函數通用:類型約束解決方案的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn