duangutang3940 2019-03-19 01:07
浏览 22
已采纳

“组合文字中的缺少类型”用于结构中的地图的匿名列表

Edit: While the compilation error in Missing type in composite literal is the same as that in my question, they were different enough in composition for me to not understand how I would apply the solution to my program, hence the creation of this question.

I am new to go, and I am trying to write a test for a function which I have verified can successfully can be called like so:

func main() {

    items := []map[string]int{
        map[string]int{
            "value": 100,
            "weight": 5,
        },
        map[string]int{
            "value": 90,
            "weight": 2,
        },
        map[string]int{
            "value": 80,
            "weight": 2,
        },
    }
    fmt.Println(KnapSack(items, 0, 6))
}

Using this template (generated by my IDE) for convenience:

func TestKnapSack(t *testing.T) {
    type args struct {
        items            []map[string]int
        current_index    int
        remaining_weight int
    }
    tests := []struct {
        name string
        args args
        want int
    }{
        {
            "Only test", // name of test
            {
                {   // items
                    map[string]int{
                        "value": 100,
                        "weight": 5,
                    },
                    map[string]int{
                        "value": 90,
                        "weight": 2,
                    },
                    map[string]int{
                        "value": 80,
                        "weight": 2,
                    },
                },
                0, // current_index
                4, // remaining_weight
            },
            170, // want
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            if got := KnapSack(tt.args.items, tt.args.current_index, tt.args.remaining_weight); got != tt.want {
                t.Errorf("KnapSack() = %v, want %v", got, tt.want)
            }
        })
    }
}

The args struct does not like my array of maps. How can I fill this struct so that it will compile?

  • 写回答

1条回答 默认 最新

  • dongtong2021 2019-03-19 02:24
    关注

    seems you miss the type of args and []map[string]int

        tests := []struct {
            name string
            args args
            want int
        }{
            {
                "Only test", // name of test
                args{
                    []map[string]int{   // items
                        map[string]int{
                            "value": 100,
                            "weight": 5,
                        },
                        map[string]int{
                            "value": 90,
                            "weight": 2,
                        },
                        map[string]int{
                            "value": 80,
                            "weight": 2,
                        },
                    },
                    0, // current_index
                    4, // remaining_weight
                },
                170, // want
            },
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大