|
|
|
|
|
by pjmlp
1825 days ago
|
|
Even though I have bashed Go's lack of generics throughout its existence, this point no longer applies, package main
import (
"fmt"
)
func Id[T any] (a T) T{
return a
}
type Stack[T any] struct {
items []T
}
type Num interface {
type int, int16, int32
}
func add3[T Num](a T, b T, c T) T {
return a + b + c
}
type LinkedList[T any] struct {
value T
next *LinkedList[T]
}
func (oldNode *LinkedList[T]) prepend(value T) *LinkedList[T] {
return &LinkedList[T]{value, oldNode}
}
func tail[T any] (value T) *LinkedList[T] {
return &LinkedList[T]{value, nil}
}
func traverse[T any](ll *LinkedList[T]) {
if ll == nil {
return
}
fmt.Println(ll.value)
traverse(ll.next)
}
func main() {
node := tail(5).prepend(6).prepend(7)
traverse(node)
}
https://go2goplay.golang.org/p/9cpHFGwuZmA |
|