Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go 测试函数不返回值_Go - Fatal编程技术网

Go 测试函数不返回值

Go 测试函数不返回值,go,Go,我正在对这个Go文件进行测试。我不明白为什么它工作起来有问题 func TestMy(t *testing.T) { arr := map[string]string{ RandomOwner(): RandomString(9), RandomOwner(): RandomString(9), RandomOwner(): RandomString(9), RandomOwner(): RandomString(9),

我正在对这个Go文件进行测试。我不明白为什么它工作起来有问题

func TestMy(t *testing.T) {
    arr := map[string]string{
        RandomOwner(): RandomString(9),
        RandomOwner(): RandomString(9),
        RandomOwner(): RandomString(9),
        RandomOwner(): RandomString(9),
        RandomOwner(): RandomString(9),
        RandomOwner(): RandomString(9),
        RandomOwner(): RandomString(9),
        RandomOwner(): RandomString(9),
        RandomOwner(): RandomString(9),
        RandomOwner(): RandomString(9),
    }
    ds := DataSourceStr{
        data: arr,
    }

    for k, v := range ds.data {
        //create dataset in datasource value method
        res, err := ds.Value(k)
        fmt.Println(res)
        if err != nil {
            t.Errorf("%T does not implement Value method correctly, key %v not found", ds, k)
        }
        if res != v {
            t.Errorf("%T does not implement Value correctly. Expected %v but recieved %v: %v", ds, v, res, err)
        }
        return res, err
    }
} 
对于上述函数,它将此作为错误返回

/home/incompleteness_hewlbern/Documents/Code_Projects/Tests/nearmap/Private_test_golang/datasource/datasource_test.go:64:3: too many arguments to return
    have (interface {}, error)
    want ()

我犯了什么错误?我应该在函数中设置类型吗?我如何简单地打印函数内部的内容,以测试它在控制台中似乎没有显示的内容。

测试函数不返回任何内容,测试通过或失败都不符合预期,在这种情况下,t.Errorf用于向go test执行的测试驱动程序发回故障信号

在您的示例中,函数具有不返回任何内容的签名:func TestMyt*testing.T{,但尝试返回接口{},error,因此是错误。将循环的最后一行return res、err更改为return以满足测试函数的签名,并根据所需的测试逻辑提前失败或完全删除该行将解决此问题。

您的函数签名是func TestMyt*testing.T{。请注意,这里没有返回值。因此,由于明显的原因,尝试返回某些内容将失败。