sfdcnoob
sfdcnoob

Reputation: 787

Variadic slices as parameter error:cannot initialize 2 variables with 1 value

Trying to combine multiple slices using variadic, I'm getting error: cannot initialize 2 variables with 1 value

How do I call this Combine function?

Here's the code:

func Combine(ss ...[]string) []string {
    mp := map[string]bool{}

    for _, s := range ss {
        for _, v := range s {
            if v != "" {
                if _, ok := mp[v]; !ok {
                    mp[v] = true
                }
            }
        }
    }

    combined := []string{}

    for v := range mp {
        combined = append(combined, v)
    }

    return combined
}



tests := []struct {
        caseName string
        s1       []string
        s2       []string
        want     []string
    }{
        {
            caseName: "Test combining 2 slices",
            s1:       []string{"a", "b", "c", "c", ""},
            s2:       []string{"a", "b", "z", "z", "", "y"},
            want:     []string{"a", "b", "c", "y", "z"},
        },
    }


actual, _ := Combine(test.s1, test.s2)

Upvotes: 1

Views: 15148

Answers (1)

colm.anseo
colm.anseo

Reputation: 22097

Your variadic calling parameters format is fine.

The error is due to your function Combine returning one item, not two:

// actual, _ := Combine(test.s1, test.s2) // fails as only one item is returned

actual := Combine(test.s1, test.s2)

Upvotes: 7

Related Questions