douqiu0351 2015-01-26 20:08
浏览 51
已采纳

GO中的测试列表

I'm trying to implement a test in GO. But I'm struggling with the list's syntax inside the struct.

package primeFactor

import "testing"

var testCases = []struct {
    p        int
    expected []int
}{
    {15, [3,5]},
    {26, [2,13]},
    {37, [37]},
    {42, [2,3,7]},
}

func TestPrimeFactor(t *testing.T) {
    for _, test := range testCases {
        observed := PrimeFactor(test.p)
        if observed != test.expected {
            t.Error("For p = %d, expected %t. Got %t.",
                test.p, test.expected, observed)
        }
    }
}

The output error I have is:

expected ']', found ','
: expected operand, found '{'
: expected ';', found 'for'

I appreciate your help. Thanks.

  • 写回答

3条回答 默认 最新

  • douzengjian1535 2015-01-26 21:10
    关注

    Toni's answer addresses your specific problem but to address the other issue of comparing slices you'll want to use reflect.DeepEqual

    Check out this example:

    package main
    
    import (
        "fmt"
        "reflect"
    )
    
    func main() {
        observed := []int{1, 2}
        expected := []int{1, 3}
    
        if reflect.DeepEqual(observed, expected) {
            fmt.Println("Slices are the same")
        } else {
            fmt.Println("Slices are different")
        }
    }
    

    https://play.golang.org/p/_JRQ5bqmJf

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000