Types 如何在Go中引用接口类型?

Types 如何在Go中引用接口类型?,types,interface,go,Types,Interface,Go,我有两种类型: type Routing map[string]Handler type Handler interface { Handle() } 我有一个名为MyHandler的类型,它满足接口的要求,如下所示: type MyHandler struct { } func (this *MyHandler) Handle() { // ... } // routes is created at the beginning of the program and av

我有两种类型:

type Routing map[string]Handler

type Handler interface {
    Handle()
}
我有一个名为MyHandler的类型,它满足接口的要求,如下所示:

type MyHandler struct {
}

func (this *MyHandler) Handle() {
    // ...
}
// 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()
我希望能够做到这样:

type MyHandler struct {
}

func (this *MyHandler) Handle() {
    // ...
}
// 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()
我在最后一行得到了这个错误:

routes[/route/here]不是一种类型

当我把最后一行改成

routes["/route/here"].Handle()
这显然有效。但是,它永远只使用处理程序的一个实例。。。我希望每次执行最后一行时都有一个新实例。如何在每次执行最后一行时实例化处理程序的新实例


我假设当使用新的时,旧的会在使用后被垃圾收集。请注意,我没有保存我创建的实例;我只想调用Handle方法,然后将其销毁。

new将类型作为参数,并返回指向该类型的清零值的指针。类型不是Go中的第一类值。新代码是一个内置代码,因此它不会按照其他代码的规则运行。New需要在编译时知道它将使用什么类型。无法构建类型映射

我的建议是使用一个函数来构建每种类型

type Routing map[string]func() Handler
routes := Routing {
    "/route/here": func() Handler { return new(MyHandler)},
}

routes["/route/here"]().Handle()
我们不是构建类型映射,而是构建返回所需类型的函数映射

另一种可能是使用反射,尽管我更喜欢上面的函数方法。对于这个用例,我相信这将是对reflect的滥用

type Routing map[string]reflect.Type
routes := Routing {
    "/route/here": reflect.TypeOf(MyHandler{}),
}

reflect.New(routes["/route/here"]).Interface().(Handler).Handle()

警告,如果MyHandler未实现处理程序,这将使程序崩溃。此方法放弃编译时类型检查。

new将类型作为参数,并返回指向该类型的清零值的指针。类型不是Go中的第一类值。新代码是一个内置代码,因此它不会按照其他代码的规则运行。New需要在编译时知道它将使用什么类型。无法构建类型映射

我的建议是使用一个函数来构建每种类型

type Routing map[string]func() Handler
routes := Routing {
    "/route/here": func() Handler { return new(MyHandler)},
}

routes["/route/here"]().Handle()
我们不是构建类型映射,而是构建返回所需类型的函数映射

另一种可能是使用反射,尽管我更喜欢上面的函数方法。对于这个用例,我相信这将是对reflect的滥用

type Routing map[string]reflect.Type
routes := Routing {
    "/route/here": reflect.TypeOf(MyHandler{}),
}

reflect.New(routes["/route/here"]).Interface().(Handler).Handle()

警告,如果MyHandler未实现处理程序,这将使程序崩溃。此方法放弃编译时类型检查。

也许路由应该是类型路由映射[string]func Handler,然后您可以在映射中放入一个NewMyHandler函数,该函数每次调用时都返回一个新的?好主意。你应该回答这个问题!我几乎不知道我在围棋中说的是什么,所以我害怕:P幸运的是,其他人做了所有填写细节的艰苦工作。也许路由应该是键入Routing map[string]func Handler,然后你可以在映射中放入一个NewMyHandler函数,每次调用它时都返回一个新的?好主意。你应该回答这个问题!我几乎不知道我在围棋里说的是什么,所以我不敢说:P幸运的是,其他人做了所有艰苦的工作来填充细节。似乎奇怪地让人想起了Javascript式的方法!我也更喜欢第一种方法。巧妙地使用反射,虽然它让我有点畏缩。没有办法建立类型图。反射模块中有-use Type来存储类型,反射模块中的其他函数来使用它。@newacct,这与我在第二个示例中所做的差不多。但是,这与语言中的类型不同。这似乎奇怪地让人想起Javascript式的方法!我也更喜欢第一种方法。巧妙地使用反射,虽然它让我有点畏缩。没有办法建立类型图。反射模块中有-use Type用于存储类型,反射模块中的其他函数用于存储类型。@newacct,这与我在第二个示例中所做的差不多。但是,这与在语言中使用类型不同。