1

I am a beginner of Go. I wrote this code, but an error occurred. How should I write a map which contains string and []string properties?

package main

import (
    "fmt"
)

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

    prof["me"] = map[string]string{
        "name":       "John Lennon",
        "email":      "[email protected]",
        "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)

}

This error is from 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
1
  • 1
    When using interface{}, you have to cast your values back. Also, in your case defining a struct is preferable (Go is not JS). Commented Jun 10, 2016 at 12:14

1 Answer 1

10

You're assigning the wrong kind of map. Try:

prof["me"] = map[string]interface{}{
                        ^^^^^^^^^^^ instead of string
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I also noticed my mistake after I posted this. Thank you for your reply.
10 minutes later, I will click "check mark". Thank you.
@yamachan Nothing beats rubber duck debugging!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.