Go 如何为我的数据获取此特定形状
在Go中,我使用了这个函数Go 如何为我的数据获取此特定形状,go,slice,shape,Go,Slice,Shape,在Go中,我使用了这个函数bar,err:=custplotter.NewCandlesticks(data) 从这里开始: 它希望数据具有以下形状: [{2 16435 16458 16435 16446 1}{3 16446 16458 16435.04 16455 1}.] 但我下面的代码将以此形状创建数据: [[2 16435 16458 16435 16446 1][3 16446 16458 16435.04 16455 1]…] 这给了我这个错误信息: 无法将数据(type[][
bar,err:=custplotter.NewCandlesticks(data)
从这里开始:
它希望数据具有以下形状:
[{2 16435 16458 16435 16446 1}{3 16446 16458 16435.04 16455 1}.]
但我下面的代码将以此形状创建数据:
[[2 16435 16458 16435 16446 1][3 16446 16458 16435.04 16455 1]…]
这给了我这个错误信息:
无法将数据(type[][]string
)用作custplotter.TOHLCVer
的参数中的类型custplotter.NewCandlesticks
:
[]字符串未实现custplotter.TOHLCVer(缺少Len方法)
我认为问题在于数据的形状。如何更改代码以创建所需的数据形状(使用{}而不是[])
我在Go-Literature中找到的所有切片示例仅使用[[],[]根据
似乎您需要的是一个custplotter.TOHLCVs
,它只是float64结构的一部分
:
因此,基本上,您的解决方案可以类似于:
df3 := make(TOHLCVs, 60) // create slice for 60 rows
idx := 0
for _, row := range df[1:61] { // read 60 rows
df3[idx].T, err = strconv.ParseFloat(row[28], 64)
df3[idx].O, err = strconv.ParseFloat(row[29], 64)
df3[idx].H, err = strconv.ParseFloat(row[30], 64)
df3[idx].L, err = strconv.ParseFloat(row[31], 64)
df3[idx].C, err = strconv.ParseFloat(row[32], 64)
df3[idx].V, err = strconv.ParseFloat(row[33], 64)
idx++
}
或者您也可以实现TOHLCVer
接口:)
键入SlicesOfTOHLCV[][6]float64
func(s SlicesOfTOHLCV)Len()int{
返回透镜(s)
}
func(s SlicesOfTOHLCV)TOHLCV(i int)(float64,float64,float64,float64,float64){
报税表s[i][0],s[i][1],s[i][2],s[i][3],s[i][4],s[i][5]
}
mySlice:=make(SlicesOfTOHLCV,60)
i:=0
对于_,行:=范围df[1:61]{
mySlice[i]=[6]float64{}
对于j:=0;j<6;j++{
mySlice[i][j],err=strconv.ParseFloat(行[28+j],64)
如果错误!=零{
恐慌(错误)
}
}
我++
}
edit:对不起,我在我的第一篇文章中复制/粘贴了错误的代码部分,现在已更正。感谢您的输入,我已按照上述内容修改了代码,现在我得到:无法在分配中使用第[28]行(类型字符串)作为float64类型。我的数据只是excel文件中的数字。(附言:如果我想显示我更新的代码,我应该把它放在哪里?我必须编辑我的初始帖子?或者使用“回答”按钮?@Hugues我在评论中写道,我不想费心将字符串转换为浮动,因为你可以自己做。我现在更新了代码,向你展示我是如何为错过这一部分感到抱歉的。现在它开始工作了,尽管我们的代码df3[idx].T=strconv.ParseFloat(第[28],64行)在单值上下文中返回多值strconv.ParseFloat()。我不得不这样修改它:df3[idx].T,err=strconv.ParseFloat(第[28],64行),根据我可以将您的代码标记为正确答案,但您能更新它吗?
type TOHLCVer interface {
// Len returns the number of time, open, high, low, close, volume tuples.
Len() int
// TOHLCV returns an time, open, high, low, close, volume tuple.
TOHLCV(int) (float64, float64, float64, float64, float64, float64)
}
// TOHLCVs implements the TOHLCVer interface using a slice.
type TOHLCVs []struct{ T, O, H, L, C, V float64 }
df3 := make(TOHLCVs, 60) // create slice for 60 rows
idx := 0
for _, row := range df[1:61] { // read 60 rows
df3[idx].T, err = strconv.ParseFloat(row[28], 64)
df3[idx].O, err = strconv.ParseFloat(row[29], 64)
df3[idx].H, err = strconv.ParseFloat(row[30], 64)
df3[idx].L, err = strconv.ParseFloat(row[31], 64)
df3[idx].C, err = strconv.ParseFloat(row[32], 64)
df3[idx].V, err = strconv.ParseFloat(row[33], 64)
idx++
}
type SlicesOfTOHLCV [][6]float64
func (s SlicesOfTOHLCV) Len() int {
return len(s)
}
func (s SlicesOfTOHLCV) TOHLCV(i int) (float64, float64, float64, float64, float64) {
return s[i][0], s[i][1], s[i][2], s[i][3], s[i][4], s[i][5]
}
mySlice := make(SlicesOfTOHLCV, 60)
i := 0
for _, row := range df[1:61] {
mySlice[i] = [6]float64{}
for j := 0; j < 6; j ++ {
mySlice[i][j], err = strconv.ParseFloat(row[28+j], 64)
if err != nil {
panic(err)
}
}
i ++
}