I have these two types:
type Routing map[string]Handler
type Handler interface {
Handle()
}
I have a type called MyHandler
which satisfies the interface, and it looks like this:
type MyHandler struct {
}
func (this *MyHandler) Handle() {
// ...
}
I'd like to be able to do something like this:
// routes is created at the beginning of the program and available
// throughout the lifetime of the script
routes := Routing {
"/route/here": MyHandler,
})
// ...
// and in another method, this line may be executed several times:
new(routes["/route/here"]).Handle()
I get this error on the last line:
routes["/route/here"] is not a type
When I change that last line to
routes["/route/here"].Handle()
it obviously works. However, it uses just one instance of the Handler forever... and I wish for a new instance every time that last line is executed. How can I instantiate a new instance of the Handler
each time that last line is executed?
(I assume that when using new
, the old ones will be garbage-collected after use. Notice that I'm not saving the instance I create; I only care to call the Handle()
method then have it destroyed.)