首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >语句结尾处的意外int

语句结尾处的意外int
EN

Stack Overflow用户
提问于 2018-07-19 12:13:29
回答 2查看 3K关注 0票数 0

我有点纠结于这段围棋代码。我到处找遍了,但不知道是怎么回事。

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

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

我还有第二行到最后一行代码的错误消息:

代码语言:javascript
复制
syntax error: non-declaration statement outside function body

如果这两个错误是相关的

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-19 12:48:46

因此,基于@和@aec的回答,我的代码现在如下所示,不再有错误消息:-),谢谢大家!

代码语言:javascript
复制
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++
    }
票数 0
EN

Stack Overflow用户

发布于 2018-07-19 12:27:11

函数声明需要从其他函数中移出,如

代码语言:javascript
复制
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
}

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

(我不确定你要寻找的确切逻辑-上面的代码还没有做任何事情。)我还将警告您不要创建一个界面,除非您需要它。)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51422437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档