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 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!