2017-10-28 59 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}),並認爲什麼是錯的! –