Go:地图的类型断言

我正在从 JSON 读取数据结构。有一些转换正在进行,最后我有一个struct字段,其中一个是 type interface{}。它实际上是一张地图,所以 JSON 将它放在一个map[string]inteface{}.


我实际上知道底层结构是map[string]float64并且我想这样使用它,所以我尝试做一个断言。以下代码重现了该行为:


type T interface{}


func jsonMap() T {

    result := map[string]interface{}{

        "test": 1.2,

    }

    return T(result)

}


func main() {

    res := jsonMap()


    myMap := res.(map[string]float64)


    fmt.Println(myMap)

}

我收到错误:


panic: interface conversion: main.T is map[string]interface {}, not map[string]float64

我可以执行以下操作:


func main() {

    // A first assertion

    res := jsonMap().(map[string]interface{})


    myMap := map[string]float64{

        "test": res["test"].(float64), // A second assertion

    }


    fmt.Println(myMap)

}

这工作正常,但我发现它非常难看,因为我需要重建整个地图并使用两个断言。有没有正确的方法来强制第一个断言放弃interface{}并使用float64?换句话说,做原始断言的正确方法是什么.(map[string]float64)?


料青山看我应如是
浏览 167回答 1
1回答

潇湘沐

不能键入断言map[string]interface{}到map[string]float64。您需要手动创建新地图。package mainimport (&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "fmt")var exampleResponseData = `{&nbsp; &nbsp; &nbsp; &nbsp; "Data":[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Type":"pos",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Content":{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "x":0.5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "y":0.3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Type":"vel",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Content":{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "vx":0.1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "vy":-0.2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }`type response struct {&nbsp; &nbsp; Data []struct {&nbsp; &nbsp; &nbsp; &nbsp; Type&nbsp; &nbsp; string&nbsp; &nbsp; &nbsp; &nbsp; Content interface{}&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; var response response&nbsp; &nbsp; err := json.Unmarshal([]byte(exampleResponseData), &response)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Cannot process not valid json")&nbsp; &nbsp; }&nbsp; &nbsp; for i := 0; i < len(response.Data); i++ {&nbsp; &nbsp; &nbsp; &nbsp; response.Data[i].Content = convertMap(response.Data[i].Content)&nbsp; &nbsp; }}func convertMap(originalMap interface{}) map[string]float64 {&nbsp; &nbsp; convertedMap := map[string]float64{}&nbsp; &nbsp; for key, value := range originalMap.(map[string]interface{}) {&nbsp; &nbsp; &nbsp; &nbsp; convertedMap[key] = value.(float64)&nbsp; &nbsp; }&nbsp; &nbsp; return convertedMap}你确定你不能定义Content为 map[string]float64?请参阅下面的示例。如果没有,你怎么知道你可以首先投射它?type response struct {&nbsp; &nbsp; Data []struct {&nbsp; &nbsp; &nbsp; &nbsp; Type&nbsp; &nbsp; string&nbsp; &nbsp; &nbsp; &nbsp; Content map[string]float64&nbsp; &nbsp; }}var response responseerr := json.Unmarshal([]byte(exampleResponseData), &response)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go