package main
import "fmt"
func MapKeys[K comparable, V any](m map[K]V) []K {
r := make([]K, 0, len(m))
fmt.Printf("001%v,%T\n", r, r)
for k := range m {
r = append(r, k)
}
fmt.Printf("002%v,%T\n", r, r)
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))
a := MapKeys[int, string](m)
fmt.Printf("aaa>>%v,%T\n", a, a)
lst := List[int]{}
lst.Push(10)
lst.Push(13)
lst.Push(23)
fmt.Println("list:", lst.GetAll())
}
输出:
001[],[]int
002[1 2 4],[]int
keys m: [1 2 4]
001[],[]int
002[2 4 1],[]int
aaa>>[2 4 1],[]int
list: [10 13 23]
标签:head,int,fmt,tail,element,链表,泛型,lst,go
From: https://www.cnblogs.com/heris/p/17212788.html