0

I want to set an instance of a some type as an element in an associative array. What type should I use?

var objects //???

//The constructor will return instance of the IndexController type
objects["IndexController"] = index.Constructor()

fmt.Println(objects)

I will be thankful!

1 Answer 1

2

Go maps are generally homogenous (each value is of the same type). If you want a different type per index, you can make an array of some interface that all of the objects in the array support. If you don't need the objects to support any methods at all, you can use the empty interface interface{}.

objects := make(map[string]interface{})
objects["IndexController"] = somethingThatReturnsAnIndexController()
Sign up to request clarification or add additional context in comments.

2 Comments

Just a comment: Go is strongly typed, so even if you can use interface{}, you will need to specify the type when you want to actually use it.
@siritinga or do a type switch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.