Maison > développement back-end > Golang > le corps du texte

Utilisez any pour instancier une interface commune, struct ne l'implémente pas

WBOY
Libérer: 2024-02-08 22:51:08
avant
947 Les gens l'ont consulté

Utilisez any pour instancier une interface commune, struct ne limplémente pas

php小编柚子在这篇文章中将为大家介绍使用any实例化通用接口的方法,并解释为什么struct并不实现它。通用接口是一种非常有用的设计模式,它允许我们在编写代码时使用更加灵活的方式。然而,在使用通用接口时,我们需要注意一些细节,以避免出现不必要的问题。让我们一起来看看如何正确地使用any实例化通用接口,并了解struct为什么不实现它。

问题内容

有人可以解释一下,为什么 *DataTo 不满足 ToType[any] 吗?

尝试构建一个 DTOer,将一个结构的所有值复制到另一个结构,并设置一些显式值(在本例中为 V

https://go.dev/play/p/-oobZrw5Ewe

// You can edit this code!
// Click here and start typing.
package main

import "fmt"

type DataFrom struct {
    V1 int
}

type DataTo struct {
    V int
}

func (m *DataTo) SetVal() {
    m.V = 1
    return
}

type ToType[T any] interface {
    SetVal()
    *T
}

type DTO[TFrom any, TTo ToType[any]] struct {
    Get func(from TFrom) TTo
}

func main() {
    dto := &DTO[DataFrom, *DataTo]{
        Get: func(from DataFrom) *DataTo {
            return &DataTo{V: from.V1 + 666}
        },
    }

    vFrom := DataFrom{V1: 1}
    vTo := dto.Get(vFrom)

    fmt.Println(vTo.V)
}
Copier après la connexion

解决方法

因为any是静态类型。

如果您使用它来实例化像 ToType 这样的泛型类型,则该泛型类型将准确地期望 any

现在,类型参数的某些用法可能会隐藏此问题,例如:

type Foo[T any] struct {
    Value T
}

Foo[any]{Value: 12} // ok
Copier après la connexion

通常情况下,您可以像上面那样为 any 指定任何类型,因为 any 只是空接口 interface{} 的别名,并且任何类型都满足空接口。

当类型参数用于复合类型(例如 *T)时,使用 any 实例化意味着完全是 *any。因此,您可以将 ToType[any] 想象为相同的东西:

type ToTypeAny interface {
    SetVal()
    *any
}
Copier après la connexion

然后 *DataTo 显然不是 *any。更多详细信息:将结构指针分配给接口指针

如果您按如下方式声明该结构,它将编译:

type DTO[TFrom any, TTo ToType[DataTo]] struct {
    Get func(from TFrom) TTo
}
Copier après la connexion

或者以更“通用”但也更详细的方式:

type DTO[TFrom any, T any, TTo ToType[T]] struct {
    Get func(from TFrom) TTo
}

&DTO[DataFrom, DataTo, *DataTo]{ ... }
Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:stackoverflow.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!