Golang:如何编写一个混合了字符串和数组的地图?

我是初学者Go。我写了这段代码,但是发生了错误。我应该如何编写包含string和[]string属性的地图?


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)


}

此错误来自Ideone。


# _/home/NcWlmE

./prog.go:14: cannot use []string literal (type []string) as type string in map value

./prog.go:15: cannot use []string literal (type []string) as type string in map value

./prog.go:16: cannot use []string literal (type []string) as type string in map value


PIPIONE
浏览 203回答 1
1回答

杨魅力

您分配了错误类型的地图。尝试:prof["me"] = map[string]interface{}{                         ^^^^^^^^^^^ instead of string
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go