donglin7979 2019-04-11 22:31
浏览 2836
已采纳

可变参数切片作为参数错误:无法初始化具有1个值的2个变量

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)
  • 写回答

1条回答 默认 最新

  • douxidao3524 2019-04-11 23:05
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数据结构中的数组地址问题
  • ¥15 LD衰减怎么根据纵坐标计算横坐标的值
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 Python题,不会啦
  • ¥15 Python题,回答一下下啦
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题
  • ¥20 oracle RAC 怎么配置啊,配置
  • ¥15 excel 日常使用中出现问题