Golang接口和转换

Rya*_* Yu 1 go

我有以下代码:

   func returnTheMap() map[string][]string{
        myThing := getSomeValue()
    }
Run Code Online (Sandbox Code Playgroud)

getSomeValue() 返回类型的东西 map[string]interface{}

但它始终在内部map[string][]string.

什么是设置myThing相同的最佳方式getSomeValue(),但类型map[string][]string

我可以像这样制作一个新对象:

newMap := make(map[string][]string)

// cardTypeList is of type map[string]interface {}, must convert to map[string][]string
for k, v := range myThing {
    newMap[k] = v.([]string)
}
Run Code Online (Sandbox Code Playgroud)

但有没有办法在这里做到这一点,或者有任何首选的方法来做到这一点?

小智 6

根据Go FAQ,您无法直接更改切片的类型.

因此,您的解决方案似乎是首选方式.