github.com/traefik/yaegi@v0.15.1/_test/gen4.go (about)

     1  package main
     2  
     3  import "fmt"
     4  
     5  type List[T any] struct {
     6  	head, tail *element[T]
     7  }
     8  
     9  // A recursive generic type.
    10  type element[T any] struct {
    11  	next *element[T]
    12  	val  T
    13  }
    14  
    15  func (lst *List[T]) Push(v T) {
    16  	if lst.tail == nil {
    17  		lst.head = &element[T]{val: v}
    18  		lst.tail = lst.head
    19  	} else {
    20  		lst.tail.next = &element[T]{val: v}
    21  		lst.tail = lst.tail.next
    22  	}
    23  }
    24  
    25  func (lst *List[T]) GetAll() []T {
    26  	var elems []T
    27  	for e := lst.head; e != nil; e = e.next {
    28  		elems = append(elems, e.val)
    29  	}
    30  	return elems
    31  }
    32  
    33  func main() {
    34  	lst := List[int]{}
    35  	lst.Push(10)
    36  	lst.Push(13)
    37  	lst.Push(23)
    38  	fmt.Println("list:", lst.GetAll())
    39  }
    40  
    41  // Output:
    42  // list: [10 13 23]