Golang:我怎么能写出一个与字符串和数组混合的地图?

yam*_*han 0 dictionary go

我是初学者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)

}
Run Code Online (Sandbox Code Playgroud)

此错误来自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
Run Code Online (Sandbox Code Playgroud)

cni*_*tar 5

你正在分配错误的地图.尝试:

prof["me"] = map[string]interface{}{
                        ^^^^^^^^^^^ instead of string
Run Code Online (Sandbox Code Playgroud)

  • @yamachan没什么比[橡皮鸭调试](https://en.wikipedia.org/wiki/Rubber_duck_debugging)! (2认同)