Dictionary 是否有一种方法可以使用映射和接口在Go中转换不同包中的结构?

Dictionary 是否有一种方法可以使用映射和接口在Go中转换不同包中的结构?,dictionary,go,struct,type-conversion,Dictionary,Go,Struct,Type Conversion,我想在不同的包之间转换一些结构类型,然后对包中的结构进行操作,而不使用if/else或switch语句。仅使用映射和接口就可以做到这一点吗?我对如何做到这一点有一些想法 包装其他 包装矿 这里,我从string到ConversionFunction的映射让我可以选择要在运行时使用的转换函数。如果FirstUpdateConverted和SecondUpdateConverted都实现了ServiceCaller接口,我也可以选择在运行时如何处理转换后的结构 但是,为了从FirstUpdate转换

我想在不同的包之间转换一些结构类型,然后对包中的结构进行操作,而不使用
if/else
switch
语句。仅使用
映射
接口
就可以做到这一点吗?我对如何做到这一点有一些想法

包装其他 包装矿 这里,我从
string
ConversionFunction
的映射让我可以选择要在运行时使用的转换函数。如果
FirstUpdateConverted
SecondUpdateConverted
都实现了
ServiceCaller
接口,我也可以选择在运行时如何处理转换后的结构

但是,为了从
FirstUpdate
转换为
FirstUpdateConverted
(或任何其他别名类型),我需要使用
开关
并手动转换


是否有其他方法执行结构转换步骤?

为什么要重命名包中其他包中的类型?这通常不是您想要做的事情。我想向这些类型添加额外的方法,而且似乎我可以使用别名或嵌入。我的包中的别名将不会导出。类型“别名”还不存在(在go1.9中出现),重命名类型将创建一个具有新方法集的全新类型。您几乎总是希望使用嵌入来添加到方法集。我以前看到过这个页面:。但是我想我看到了我想做的是不可能的,因为Go的静态类型(使用反射将接口转换为结构)我不得不同意这里存在一些设计缺陷,但是如果没有更多的上下文,我不能具体说什么。看起来你可能在建筑上把自己画进了一个角落。这看起来是一个潜在的问题。
type Update interface{}

type FirstUpdate struct {
    x int
    y int
}

type SecondUpdate struct {
    z string
}

// these methods return different implementations of Update structs
FirstRequestConverter(events []string, topic string) Update
SecondRequestConverter(events []string, topic string) Update
type ConversionFunction func([]string, string) other.Update

type FirstUpdateConverted other.FirstUpdate
type SecondUpdateConverted other.SecondUpdate

type ServiceCaller interface {
    // includes relevant methods
}    

topicsMap := map[string]ConversionFunction{
    "first_topic": other.FirstRequestConverter,
    "second_topic": other.SecondRequestConverter,
}

conversionFunction = topicsMap[topic]
// need to convert other package types to alias types
...
updateConverted.callService()