小编alg*_*cus的帖子

返回泛型类型的默认值

如何返回nil泛型类型T

func (list *mylist[T]) pop() T {
    if list.first != nil {
        data := list.first.data
        list.first = list.first.next
        return data
    }
    return nil
}

func (list *mylist[T]) getfirst() T {
    if list.first != nil {
        return list.first.data
    }
    return nil
}

Run Code Online (Sandbox Code Playgroud)

我收到以下编译错误:

 cannot use nil as T value in return statement
Run Code Online (Sandbox Code Playgroud)

generics null go

63
推荐指数
2
解决办法
3万
查看次数

标签 统计

generics ×1

go ×1

null ×1