gpt4 book ai didi

go - 数据库和上下文错误太多

转载 作者:数据小太阳 更新时间:2023-10-29 03:33:02 25 4
gpt4 key购买 nike

我在“go”代码下运行并收到很多错误:

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\n", 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\n", 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\n", 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\n", err)
}
ctx.ViewData("blog", blogData)
ctx.View("homepage.html")
}
}

错误是:-

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

所有值都按照以下所述获取:http://go-database-sql.org/retrieving.html ,仍然不确定为什么 undefined variable 问题。我认为不需要创建这些变量,但如果必须创建,请告诉我。

谢谢

最佳答案

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

homePage 是 void 函数。所以你不能在函数参数中使用它。

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

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

这是错误的语法。我建议你去https://tour.golang.org/

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

正如我在上面所说的,homePage 是 void 函数。所以你不能使用 return with value。以下是我认为您的代码应该是的代码。

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\n", 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\n", 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\n", 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\n", err)
}
ctx.ViewData("blog", blogData)
ctx.View("homepage.html")
}
}

关于go - 数据库和上下文错误太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44915201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com