duanlinzhen7235 2017-07-04 23:33
浏览 8
已采纳

数据库和上下文太多错误

I am running below "go" code and getting lots of error :

package main

import (
    "database/sql"
    "log"    
    "github.com/get-ion/ion"
    "github.com/get-ion/ion/context"
    "github.com/get-ion/ion/view"
    _ "github.com/go-sql-driver/mysql"
)

func main() {
    app := ion.New()
    app.RegisterView(view.HTML("./templates", ".html"))

    db, err := sql.Open("mysql", "root:password@/database?charset=utf8&parseTime=true&loc=Local")
    if err != nil {
        log.Fatalln(err)
        panic("There was an error handling mysql connection!")
    }
    defer db.Close()

    allRoutes := app.Party("/", logThisMiddleware)
    {
        allRoutes.Get("/", homePage(db))
    }
}

func logThisMiddleware(ctx context.Context) {
    ctx.Application().Logger().Infof("Path: %s | IP: %s
", ctx.Path(), ctx.RemoteAddr())
    ctx.Next()
}

func homePage(db *sql.DB) {
    func(ctx context.Context) {
       var (
            id             int
            title          string
            featured_image string
            created_at     string
        )
        rows, err := db.Query("SELECT id, title, featured_image, created_at from blogs order by crated_at desc limit 0,5")
        if err != nil {
            ctx.Application().Logger().Fatalf("MySQL Error fetching row %s
", err)
        }
        defer rows.Close()
        blogData := map[int]map[int]string{}
        for rows.Next() {
            err := rows.Scan(&id, &title, &featured_image, &created_at)
            if err != nil {
                ctx.Application().Logger().Fatalf("Error while fetching row from blog: %s
", err)
            }
            blogData[id][0] = title
            blogData[id][1] = featured_image
            blogData[id][2] = created_at
        }
        err = rows.Err()
        if err != nil {
            ctx.Application().Logger().Fatalf("Error while scanning Row : %s
", err)
        }
        ctx.ViewData("blog", blogData)
        ctx.View("homepage.html")
    }
}

Errors are :-

./main.go:40: homePage(db) used as value
./main.go:50: func literal evaluated but not used

All the values are fetched as mentioned in : http://go-database-sql.org/retrieving.html , Still not sure why undefined variable issue. I don't think, these variables are required to be created, but if in case it has to be , please let me know.

Thanks

  • 写回答

1条回答 默认 最新

  • dongsuishou8039 2017-07-04 23:54
    关注

    ./main.go:40: homePage(db) used as value

    homePage is void function. So you can't use it in function arguments.

    ./main.go:50: func literal evaluated but not used

    func homePage(db *sql.DB) {
        func(ctx context.Context) {
            ...
        }
        return ""
    }
    

    This is broken syntax. I suggest you to go https://tour.golang.org/

    ./main.go:79: too many arguments to return have (string) want ()

    As I said in above, homePage is void function. So you can't use return with value. Below is code which I could suppose your code should be.

    package main
    
    import (
        "database/sql"
        "log"
    
        "github.com/get-ion/ion"
        "github.com/get-ion/ion/context"
        "github.com/get-ion/ion/view"
        _ "github.com/go-sql-driver/mysql"
    )
    
    func main() {
        app := ion.New()
        app.RegisterView(view.HTML("./templates", ".html"))
    
        db, err := sql.Open("mysql", "root:password@/database?charset=utf8&parseTime=true&loc=Local")
        if err != nil {
            log.Fatalln(err)
            panic("There was an error handling mysql connection!")
        }
        defer db.Close()
    
        allRoutes := app.Party("/", logThisMiddleware)
        {
            allRoutes.Get("/", homePage(db))
        }
    
        //app.Run(ion.Addr(":8080"))
    }
    
    func logThisMiddleware(ctx context.Context) {
        ctx.Application().Logger().Infof("Path: %s | IP: %s
    ", ctx.Path(), ctx.RemoteAddr())
        ctx.Next()
    }
    
    func homePage(db *sql.DB) context.Handler {
        return func(ctx context.Context) {
            var (
                id             int
                title          string
                featured_image string
                created_at     string
            )
            rows, err := db.Query("SELECT id, title, featured_image, created_at from blogs order by crated_at desc limit 0,5")
            if err != nil {
                ctx.Application().Logger().Fatalf("MySQL Error fetching row %s
    ", err)
            }
            defer rows.Close()
            blogData := map[int]map[int]string{}
            for rows.Next() {
                err := rows.Scan(&id, &title, &featured_image, &created_at)
                if err != nil {
                    ctx.Application().Logger().Fatalf("Error while fetching row from blog: %s
    ", err)
                }
                blogData[id][0] = title
                blogData[id][1] = featured_image
                blogData[id][2] = created_at
            }
            err = rows.Err()
            if err != nil {
                ctx.Application().Logger().Fatalf("Error while scanning Row : %s
    ", err)
            }
            ctx.ViewData("blog", blogData)
            ctx.View("homepage.html")
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程