Home > Backend Development > Golang > **How to Preserve Interface-Implementor Relationships When Using Generics in Go for Slice Initialization?**

**How to Preserve Interface-Implementor Relationships When Using Generics in Go for Slice Initialization?**

DDD
Release: 2024-10-25 17:22:02
Original
566 people have browsed it

**How to Preserve Interface-Implementor Relationships When Using Generics in Go for Slice Initialization?**

Interface/Implementation Simultaneity in Golang Generics

Consider the task of creating a generic function to fill a slice with initialized values. While this may seem straightforward, challenges arise when attempting to utilize slices of interfaces and specify concrete types within the function.

In Go 1.18, constraining both X and Y as any types within the generic function Fill leads to a loss of any relationship between the interface and its implementors. This prevents the assignment of instances of Y to slice[i] within the function.

To overcome this, an explicit assertion can be used:

<code class="go">func Fill[X, Y any](slice []X) {
    for i := range slice {
        slice[i] = any(*new(Y)).(X)
    }
}</code>
Copy after login

However, this approach panics when Y does not implement X. Additionally, using a pointer type for Y results in the loss of information about the base type and a nil baseline value.

To address these issues, it is recommended to use a constructor function instead of a second type parameter:

<code class="go">func main() {
    xs := make([]sync.Locker, 10)
    Fill(xs, func() sync.Locker { return &sync.Mutex{} })
}

func Fill[X any](slice []X, f func() X) {
    for i := range slice {
        slice[i] = f()
    }
}</code>
Copy after login

This approach effectively fills the slice with initialized values created by the constructor function, providing a more robust and flexible solution to the problem.

The above is the detailed content of **How to Preserve Interface-Implementor Relationships When Using Generics in Go for Slice Initialization?**. 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