提问人:David 提问时间:10/13/2023 最后编辑:Peter MortensenDavid 更新时间:10/15/2023 访问量:110
为什么 Go 不能转换一个实现泛型的接口?
Why can't Go cast back an interface that implements a generic?
问:
我正在尝试探索 Go 的类型系统,在编写一个小的副项目时很开心,但我最终遇到了一个奇怪的情况。
当一个可以接受一个类型时,在该类型中将其用于函数,一个实现包含在 映射中的东西,当检索时,我无法将其转换回实现。为什么?如何?怎么了?interface
struct
interface
interface
package main
import (
"context"
"fmt"
)
type State struct {
Data string
}
type InterfaceFuncs[T any] interface {
Init(ctx context.Context,
stateGetter func() T,
stateMutator func(mutateFunc func(T) T)) error
}
type ConfigWrap[T any] struct {
InterFuncs InterfaceFuncs[T]
}
type Controller[T any] struct {
mapinterfaces map[string]ConfigWrap[T]
}
func New[T any](initialState T) *Controller[T] {
return &Controller[T]{
mapinterfaces: make(map[string]ConfigWrap[T]),
}
}
func (c *Controller[T]) RegisterFuncs(pid string, config ConfigWrap[T]) error {
c.mapinterfaces[pid] = config
return nil
}
func (c *Controller[T]) InterFuncs(pid string) (*InterfaceFuncs[T], error) {
var pp ConfigWrap[T]
var exists bool
if pp, exists = c.mapinterfaces[pid]; exists {
return &pp.InterFuncs, nil
}
return nil, fmt.Errorf("InterFuncs not found")
}
func main() {
ctrl := New[State](State{})
ctrl.RegisterFuncs("something", ConfigWrap[State]{
InterFuncs: &ImpltProcFuncs{
Data: "get me back!!!!",
},
})
// why can't we cast it back to ImpltProcFuncs
getback, _ := ctrl.InterFuncs("something")
// I tried to put it as interface but doesn't works either
//// doesn't work
switch value := any(getback).(type) {
case ImpltProcFuncs:
fmt.Println("working", value)
default:
fmt.Println("nothing")
}
//// doesn't work
// tryme := any(getback).(ImpltProcFuncs) // panic: interface conversion: interface {} is *main.InterfaceFuncs[main.State], not main.ImpltProcFuncs
// fmt.Println("please", tryme.Data)
//// doesn't work
// tryme := getback.(ImpltProcFuncs)
//// doesn't work
// switch value := getback.(type) {
// case ImpltProcFuncs:
// fmt.Println("working", value)
// }
}
type ImpltProcFuncs struct {
Data string
}
func (p *ImpltProcFuncs) Init(
ctx context.Context,
stateGetter func() State,
stateMutator func(mutateFunc func(State) State)) error {
return nil
}
我怎样才能把我的背部作为一个变量来获得?ImpltProcFuncs
Data
我错过了什么?
我以为 Go 能够从 .interface
答:
-6赞
David
10/13/2023
#1
好吧,在四处挖掘之后,你可以感谢 ChatGPT 和 Bing......
if impl, ok := (*getback).(*ImpltProcFuncs); ok {
fmt.Println("working", impl.Data)
} else {
fmt.Println("not working")
}
执行时,它会输出“working get me back!!!”
评论
1赞
Brits
10/13/2023
备选方案(指向接口的指针很少有用func (c *Controller[T]) InterFuncs(pid string) (InterfaceFuncs[T], error)
)
评论