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

DDD
Release: 2024-10-27 02:28:30
Original
353 people have browsed it

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

Generic Method Parameters in Go: Solving the Issue of Type Constraints

Problem:

Consider the following Go code:

<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>
Copy after login

This code fails to compile with the error error prog.go:39: cannot use humans (type []Human) as type []Mammal in argument to Count. The issue arises because the Count function expects an array of Mammal structs, but we are passing an array of Human structs. How can we resolve this type constraint and make the Count function generic enough to accept any type that has an ID property?

Solution:

1. Use Interfaces:

Replace the concrete types with interfaces that define the ID property. For example:

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

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

2. Embed Interfaces:

To avoid duplicating the ID method in both the Mammal and Human interfaces, use embedded interfaces:

<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>
Copy after login

3. Update Count Function:

Modify the Count function to use the Mammal interface instead of the concrete Mammal type:

<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>
Copy after login

4. Create Interface-Compliant Slices:

Create slices that implement the Mammal interface:

<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>
Copy after login

5. Example Usage:

Example usage that now compiles successfully:

<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>
Copy after login

By using interfaces and embedded interfaces, we have made the Count function generic enough to handle any type that implements the Mammal interface, effectively solving the type constraint issue.

The above is the detailed content of How to Make Go Functions Generic Using Interfaces: A Type Constraint Solution. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!