dongxuxian1123 2018-07-19 12:13
浏览 157
已采纳

语句末尾出现意外的int

I'm struggling a bit with this piece of Go code. I have been searching all over the place, but can't understand what is wrong about it.

Error message is: syntax error: unexpected int at end of statement

for that line near the bottom: func (TOHLCV TOHLCVs) Len() int {

I also have this error message for the second to the last line of code:

syntax error: non-declaration statement outside function body

In case the 2 errors are related

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

this code is adapted from: https://github.com/pplcc/plotext/blob/master/custplotter/tohlcv.go

  • 写回答

2条回答 默认 最新

  • douchenchepan6465 2018-07-19 12:48
    关注

    So based on answer of @Tyler Bui-Palsulich and @aec my code now looks like below, and no more error messages :-), thanks all !

    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++
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流