I created a MapKeys generic and List generic, but when using List generic with normal map[string]List[int], I can't call the generic's method, what I am wrong? any ideas will be very appreciated!
generic code like this :
package main
import "fmt"
func MapKeys[K comparable, V any](m map[K]V) []K {
r := make([]K, 0, len(m))
for k := range m {
r = append(r, k)
}
return r
}
type List[T any] struct {
head, tail *element[T]
}
type element[T any] struct {
next *element[T]
val T
}
func (lst *List[T]) Push(v T) {
if lst.tail == nil {
lst.head = &element[T]{val: v}
lst.tail = lst.head
} else {
lst.tail.next = &element[T]{val: v}
lst.tail = lst.tail.next
}
}
func (lst *List[T]) GetAll() []T {
var elems []T
for e := lst.head; e != nil; e = e.next {
elems = append(elems, e.val)
}
return elems
}
func main() {
var m = map[int]string{1: "2", 2: "4", 4: "8"}
fmt.Println("keys m:", MapKeys(m))
_ = MapKeys[int, string](m)
var yx = make(map[string]List[int])
yx["one"] = List[int]{}
yx["one"].Push(10)
lst := List[int]{}
lst.Push(10)
lst.Push(13)
lst.Push(23)
fmt.Println("list:", lst.GetAll())
}
call this line :
yx["one"].Push(10)
got the error message :
./prog.go:49:12: cannot call pointer method Push on List[int]
you can play the code here