Golang 中的通用结构/接口列表
php小编百草在这篇文章中将为大家介绍Golang中的通用结构/接口列表。Golang是一种开源的编程语言,具有简单易学、高效可靠的特点,被广泛应用于网络编程、云计算等领域。在Golang中,通用结构和接口是非常重要的概念,可以帮助我们实现代码的复用性和扩展性。通过本文的介绍,相信读者们能够更好地理解和应用Golang中的通用结构和接口,提升自己的编程技能。
问题内容
有没有办法在 go 中获得通用结构/接口的列表?
这就是我想要实现的目标。
package main
type List[T any] struct {
Elements []T
}
func (f *List[T]) Add(el T) {
f.Elements = append(f.Elements, el)
}
type ListInterface[T any] interface {
Add(el T)
}
func main() {
listOfLists := make([]ListInterface[any], 0)
listOfLists = append(listOfLists, &List[int]{})
}
登录后复制