语句末尾出现意外的int

时间:2018-07-19 12:13:29

标签: go struct slice

我在这段Go代码中苦苦挣扎。我一直在到处搜索,但是不明白这是怎么回事。

错误消息是:语法错误:语句末尾出现意外的int

靠近底部的那一行:func (TOHLCV TOHLCVs) Len() int {

对于第二行到最后一行代码,我也有此错误消息:

syntax error: non-declaration statement outside function body

如果2个错误相关联

package main

import (
    "fmt"
    "time"
    "strconv"

    //from https://github.com/pplcc/plotext/
    "log"
    "os"

    "github.com/360EntSecGroup-Skylar/excelize"
    "github.com/pplcc/plotext/custplotter"
    "gonum.org/v1/plot"
    "github.com/pplcc/plotext"
    "gonum.org/v1/plot/vg/vgimg"
    "gonum.org/v1/plot/vg/draw"
)

    // Len implements the Len method of the TOHLCVer interface.
    func (TOHLCV TOHLCVs) Len() int {
        return len(TOHLCV)

func main() {

//read excel file******************************************
    xlsx, err := excelize.OpenFile("/media/Snaps/test snaps.xlsm")
    if err != nil {
        fmt.Println(err)
        return
    }

    //read all rows into df
    df := xlsx.GetRows("ticker_2")

    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)
    }

    type TOHLCVs []struct{ T, O, H, L, C, V float64 }

    // Len implements the Len method of the TOHLCVer interface.
    func (TOHLCV TOHLCVs) Len() int {
        return len(TOHLCV)
    }

    df3 := make(TOHLCVs, 60) // create slice for 60 rows
    idx := 0

此代码改编自: https://github.com/pplcc/plotext/blob/master/custplotter/tohlcv.go

2 个答案:

答案 0 :(得分:0)

函数声明需要移出其他函数,例如this

package main

import (
    "fmt"

    "github.com/360EntSecGroup-Skylar/excelize"
)

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)
}

type TOHLCVs []struct{ T, O, H, L, C, V float64 }

// Len implements the Len method of the TOHLCVer interface.
func (TOHLCV TOHLCVs) Len() int {
    return len(TOHLCV)
}

func main() {
    //read excel file******************************************
    xlsx, err := excelize.OpenFile("/media/Snaps/test snaps.xlsm")
    if err != nil {
        fmt.Println(err)
        return
    }

    //read all rows into df
    df := xlsx.GetRows("ticker_2")

    df3 := make(TOHLCVs, 60) // create slice for 60 rows
    idx := 0
}

类型声明可以在函数内部。但是,在这种情况下,让他们在外面更有意义。在某些情况下,在另一个函数中声明一个函数会有所帮助:

(我不确定您要查找的确切逻辑-上面的代码尚未执行任何操作。我还将警告不要创建接口,除非您需要它。)

答案 1 :(得分:0)

因此,根据@Tyler Bui-Palsulich和@aec的回答,我的代码现在如下所示,并且不再显示错误消息:-),谢谢大家!

package main

import (
    "fmt"
    "time"
    "strconv"

    //from https://github.com/pplcc/plotext/
    "log"
    "os"

    "github.com/360EntSecGroup-Skylar/excelize"
    "github.com/pplcc/plotext/custplotter"
    //"github.com/pplcc/plotext/examples"
    "gonum.org/v1/plot"
    "github.com/pplcc/plotext"
    "gonum.org/v1/plot/vg/vgimg"
    "gonum.org/v1/plot/vg/draw"
)

// Len implements the Len method of the TOHLCVer interface.
//func (TOHLCV TOHLCVs) Len() int {
//    return len(TOHLCV)
//}

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)
}

type TOHLCVs []struct{ T, O, H, L, C, V float64 }

// Len implements the Len method of the TOHLCVer interface.
func (TOHLCV TOHLCVs) Len() int {
    return len(TOHLCV)
}

// TOHLCV implements the TOHLCV method of the TOHLCVer interface.
func (TOHLCV TOHLCVs) TOHLCV(i int) (float64, float64, float64, float64, float64, float64) {
    return TOHLCV[i].T, TOHLCV[i].O, TOHLCV[i].H, TOHLCV[i].L, TOHLCV[i].C, TOHLCV[i].V
}

func main() {

start := time.Now()

//create data for each chart****************************************************
//******************************************************************************

    //read excel file******************************************
    xlsx, err := excelize.OpenFile("/media/hugues/M.2 windows/Hugues/Snaps/test snaps.xlsm")
    if err != nil {
        fmt.Println(err)
        return
    }

    //read all rows into df
    df := xlsx.GetRows("ticker_2")


    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++
    }