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

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?

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.

After looking at PlatformDictStr, I think var dictionary map[string]string should do the job. PlatformID(dict["platform-id"].(string)) can be avoided in that case. Working example here https://play.golang.org/p/A5kiVm_XbP

Is there any specific reason for which you have created types for string and map? If not, I think you are over-engineering a simple use-case. The following works fine:

package main

import (
    "encoding/json"
    "fmt"
)

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

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

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

    fmt.Println(PlatformDictStr)

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

Working Playground example: https://play.golang.org/p/mpPpDmiz7x