登录
首页 >  Golang >  Go问答

在Golang中,如何将泛型类型的接口与nil进行比较?

来源:stackoverflow

时间:2024-03-31 12:54:34 238浏览 收藏

哈喽!今天心血来潮给大家带来了《在Golang中,如何将泛型类型的接口与nil进行比较?》,想必大家应该对Golang都不陌生吧,那么阅读本文就都不会很困难,以下内容主要涉及到,若是你正在学习Golang,千万别错过这篇文章~希望能帮助到你!

问题内容

我需要一个链接节点来保存一些不同的接口类型,所以我用泛型来制作它,但是泛型类型 any 不能与 nil 进行比较,它显示错误,如评论中所示:

package main

type myinterface interface {
}
type node[t any] struct {
    next *node[t]
    leaf t
}

func (n *node[t]) getfirstnodehasleaf() *node[t] {
    if n.leaf != nil { // <---- error here: cannot compare n.leaf != nil (mismatched types t and untyped nil)
        return n
    }

    if n.next == nil {
        return nil
    } else {
        return n.next.getfirstnodehasleaf()
    }
}

func main() {
    var n = &node[myinterface]{}
    // fill n with lots of nodes
    n.getfirstnodehasleaf() // get the first node that has (leaf != nil)
}

我还尝试与默认值进行比较

    var nilt t
    if n.leaf != nilt { // <-- same problem

并将节点类型限制为

type node[T myInterface] struct {

同样的错误,请问如何解决?谢谢。


正确答案


使用接口实例化像 node 这样的泛型类型可能是一个概念缺陷。所以我们先看一般情况,最后看接口情况。

使用 comparablet

如果要对类型参数类型的值使用 ==!= 等相等运算符,则约束必须为 comparable

type node[t comparable] struct {
    next *node[t]
    leaf t
}

但是您不会针对 nil 进行测试,您将针对 t 的零值进行测试,根据您实例化它的内容,该零值可能是 nil 以外的值。

在这种情况下,您可以将 t 类型的变量声明为零值:

var zero t
if n.leaf != zero {
    return n
}

但是接口类型不实现 comparable

使用 any*t

作为替代方案,您可以保留约束 any 并将字段 leaf 声明为指向 t 的指针。它支持相等运算符,因为 leaf 类型不再是类型参数,而是指针:

type node[t any] struct {
    next *node[t]
    leaf *t
}

func (n *node[t]) getfirstnodehasleaf() *node[t] {
    if n.leaf != nil { // ok, leaf is a pointer type
        return n
    }
...
}

使用 anyt

使用约束 anyt 不支持相等运算符;您可以使用任何类型实例化 node,包括那些不可比较的类型。

只要字段不是指针,就只能使用反射来检查零值(指针类型为 nil`):

if !reflect.valueof(n.leaf).iszero() {
    return n
}

最后,请考虑如果 t 是接口类型,则上述代码不起作用。测试的是界面中装箱的动态值。如果 t 确实必须是一个接口,请使用以下命令测试零值:

// leaf is an interface type
if !reflect.ValueOf(&n.leaf).Elem().IsZero() {
    return n
}

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>