Home >Backend Development >Golang >How to fix `invalid recursive type Tarea` error when trying to make `subtareas` a pointer to `ListaDeTareas` in Go?

How to fix `invalid recursive type Tarea` error when trying to make `subtareas` a pointer to `ListaDeTareas` in Go?

王林
王林forward
2024-02-11 12:30:09471browse

当尝试在 Go 中使 `subtareas` 成为指向 `ListaDeTareas` 的指针时,如何修复 `invalid recursive type Tarea` 错误?

When using Go language, when we try to declare `subtareas` as a pointer to `ListaDeTareas`, we may encounter `invalid recursive type Tarea` error. This is because the Go language has some limitations on the processing of recursive types, which requires us to make some repairs. There are many ways to solve this problem. We can use interface types or structure nesting to solve this problem. Below I will detail how to fix this error.

Question content

I need the subtareas in struct tarea as a pointer to listadetareas but it doesn't work. I have invalid recursive type tarea

type Tarea struct {
    nombre    string
    duracion  float32

    subtareas *ListaDeTareas
}

type ListaDeTareas[T Tarea] struct {
    elementos listadetareas.LinkedList[Tarea]
}

Workaround

It appears that you are not using the type parameter t in listadetareas. Removing it will solve the problem.

type Tarea struct {
    nombre    string
    duracion  float32

    subtareas *ListaDeTareas
}

type ListaDeTareas struct {
    elementos listadetareas.LinkedList[Tarea]
}

The above is the detailed content of How to fix `invalid recursive type Tarea` error when trying to make `subtareas` a pointer to `ListaDeTareas` in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete