Dictionary 葛朗:我怎样才能写出一幅混合了字符串和数组的地图呢?

Dictionary 葛朗:我怎样才能写出一幅混合了字符串和数组的地图呢?,dictionary,go,Dictionary,Go,我是围棋初学者。 我写了这段代码,但出现了一个错误。 如何编写包含字符串和[]字符串属性的映射 package main import ( "fmt" ) func main() { prof := make(map[string]map[string]interface{}) prof["me"] = map[string]string{ "name": "John Lennon", "email": "foo

我是围棋初学者。 我写了这段代码,但出现了一个错误。 如何编写包含字符串和[]字符串属性的映射

package main

import (
    "fmt"
)

func main() {
    prof := make(map[string]map[string]interface{})

    prof["me"] = map[string]string{
        "name":       "John Lennon",
        "email":      "foobar@gmail.com",
        "phone":      "090-0000-0000",
        "occupation": []string{"Programmer", "System Engineer"},
        "language": []string{"Go", "Java", "Python", "PHP", "JavaScript", "SQL"},
        "hobby": []string{"Photography", "Traveling", "Fishing", "Eating"},
    }

    fmt.Println(prof)

}
此错误来自


您分配了错误的地图类型。尝试:

prof["me"] = map[string]interface{}{
                        ^^^^^^^^^^^ instead of string

您分配了错误的地图类型。尝试:

prof["me"] = map[string]interface{}{
                        ^^^^^^^^^^^ instead of string

非常感谢。我也注意到我的错误后,我张贴了这一点。谢谢您的回复。10分钟后,我将单击复选标记。谢谢你。@yamachan什么都比不上!非常感谢。我也注意到我的错误后,我张贴了这一点。谢谢您的回复。10分钟后,我将单击复选标记。谢谢你。@yamachan什么都比不上!当使用接口{}时,必须将值转换回原来的值。另外,在您的情况下,最好定义一个结构,Go不是JS。当使用接口{}时,您必须将您的值转换回原来的值。此外,在您的情况下,最好定义一个结构,而不是JS。