jjzjj

childCollection

全部标签

go - Go中的Supertype如何引用Subtype

Go不支持多态性。如果要在通用类型的保护伞下传递特定类型,则它无法在Go中工作。以下一段代码抛出错误。在Go中实现相同功能的最佳方法是什么?packagemainimport("fmt")typeparentstruct{parentIDstring}typechild1struct{parentchild1IDstring}typechild2struct{parentchild2IDstring}typechildCollectionstruct{collection[]parent}func(c*childCollection)appendChild(pparent){c.col