我想将某种类型的实例设置为关联数组中的元素。我应该使用什么类型?
var objects //???
//The constructor will return instance of the IndexController type
objects["IndexController"] = index.Constructor()
fmt.Println(objects)
我将不胜感激!
最佳答案
Go map 通常是同质的(每个值都是同一类型)。如果你想要每个索引不同的类型,你可以创建一个数组中的所有对象都支持的一些接口(interface)。如果您根本不需要对象支持任何方法,则可以使用空接口(interface) interface{}
。
objects := make(map[string]interface{})
objects["IndexController"] = somethingThatReturnsAnIndexController()
关于go - 我可以在 Go 中创建不同对象的关联数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23173545/