2017-10-28 141 views
-1

我一直在使用Golang的“testing”包编写测试用例。而且我遇到了必须将数组和函数指针写入表的情况。在struct(Golang)中写入数组

我尝试以下操作:

type myFunctionType func([]float64, []float64) float64 
var testMatrix = []struct { 
    dataX []float64 
    dataY []float64 
    result float64 
    myFunction myFunctionType 
} { 
{ {2, 3}, {8, 7}, 1, doMagicOne}, 
    {2, 3}, {8, 7}, 1, doMagicTwo}, 
} 

但每次我最终得到时间的跟踪误差或别的东西:

missing type in composite literal

在上述任何输入?提前致谢。

回答

2

您报告的错误是由于您的数组中缺少类型声明之前的类型造成的。错误:

missing type in composite literal

指的是该位的声明:

{2, 3} 

需要指定数组类型:

[]float64{2, 3} 

因此,你需要:

var testMatrix = []struct { 
    dataX  []float64 
    dataY  []float64 
    result  float64 
    myFunction myFunctionType 
}{ 
    {[]float64{2, 3}, []float64{8, 7}, 1, doMagicOne}, 
    {[]float64{2, 3}, []float64{8, 7}, 1, doMagicTwo}, 
} 

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

+0

Kenny,谢谢你的回答。我一直在尝试[]浮动({1,2}),并认为什么是错的! –