doufan6033 2016-09-03 00:53
浏览 46
已采纳

如何将接口转换为自定义类型

I am trying to parse out a specific field from a json string, and currently I have the following code snippet.

package main

import (
    "encoding/json"
    "fmt"
)

type PlatformID string

type Map map[string]interface{}

func Str2Map(str string) (Map, error) {
    var dictionary Map
    bytes := []byte(str)
    err := json.Unmarshal(bytes, &dictionary)
    if err != nil {
        fmt.Println(err)
    }
    return dictionary, err
}

func parsePlatformID(str string) (PlatformID, error) {
    fmt.Println(str)
    dict, err := Str2Map(str)
    fmt.Println(dict)
    return dict["platform-id"].(PlatformID), err
}

func main() {
    PlatformDictStr := "{\"platform-id\":\"platform_BnjliXLEUV26\",\"platform-labels\":\"test\",\"OptimizeScheme\":\"None\"}"

    fmt.Println(PlatformDictStr)

    ID, _ := parsePlatformID(PlatformDictStr)
    fmt.Println(ID)
}

When I try to run it, it gives me the following error

{"platform-id":"platform_BnjliXLEUV26","platform-labels":"test","OptimizeScheme":"None"}
{"platform-id":"platform_BnjliXLEUV26","platform-labels":"test","OptimizeScheme":"None"}
map[platform-id:platform_BnjliXLEUV26 platform-labels:test OptimizeScheme:None]
panic: interface conversion: interface is string, not main.PlatformID

goroutine 1 [running]:
panic(0x126aa0, 0x10532300)
    /usr/local/go/src/runtime/panic.go:500 +0x720
main.parsePlatformID(0x13e6fe, 0x58, 0x0, 0x0, 0x0, 0x0)
    /tmp/sandbox256874711/main.go:26 +0x220
main.main()
    /tmp/sandbox256874711/main.go:34 +0x100

This question sort of answers why I got panic: interface conversion: interface is string

If I try to change the type assertion to string, the underlying type of PlatformID, it won't even compile tmp/sandbox325023244/main.go:26: cannot use dict["platform-id"].(string) (type string) as type PlatformID in return argument

So how should I modify the return line so that I can retrieve PlatformID?

  • 写回答

3条回答 默认 最新

  • dpz3471 2016-09-03 01:07
    关注

    After playing around with the syntax a bit more, I think I need to do both type conversion and type assertion.

    So the following line solves the problem

    return PlatformID(dict["platform-id"].(string)), err
    

    In retrospect, I need to first assert the interface type to a base type string, and from there I can just do a type conversion to PlatformID

    PS 1: The use case is that I got the raw string in the request body, REST layer will parse out certain fields in a dictionary, then forward the rest of unparsed string to API layer for further processing. The keys in dictionary vary depends on workload, so I can't really Unmarshal it to a well defined struct.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!