0

How to correctly assign a map of slices to a struct in Golang? I tried to following, but it does not work:

package main

import (
    "fmt"
)

type Test struct {
    name     string
    testCase map[string][]string
}

var t = Test{
    name: "myTest",
    testCase: map[string][]string{"key": {"value1", "value2"}}
}

func main() {

    fmt.Println(t)
}

.\main.go:14:61: syntax error: unexpected newline, expecting comma or }

4
  • 1
    testCase: map[string][]string{"key":{"value1", "value2"}} Commented Sep 10, 2022 at 8:31
  • No, still does not work. Commented Sep 10, 2022 at 8:46
  • 3
    understand the error message, missing a trailing ,. Add it Commented Sep 10, 2022 at 8:47
  • Got it, thanks. Didn't expect that, because there was nothing following. Commented Sep 10, 2022 at 8:49

1 Answer 1

1

You have to add its type as a prefix when you assign the value.

type Test struct {
    name     string
    testCase map[string][]string
}

var t = Test{
    name: "myTest",
    testCase: map[string][]string{
        "key": {"value1", "value2"},
    },
}

Don't forget to add comma separator at the end of the item, since its use vertical style map

Reference

Sign up to request clarification or add additional context in comments.

Comments

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.