我在下面跑步"去"代码并得到很多错误:
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中提到的,仍然不确定为什么未定义的变量问题。我不认为,这些变量需要创建,但如果必须如此,请告诉我。
由于
答案 0 :(得分:2)
./ main.go:40:homePage(db)用作值
homePage是无效功能。所以你不能在函数参数中使用它。
./ main.go:50:func literal已评估但未使用
func homePage(db *sql.DB) {
func(ctx context.Context) {
...
}
return ""
}
这是破解的语法。我建议你去https://tour.golang.org/
./ main.go:79:返回的参数太多了 有(字符串) 想要()
正如我上面所说,homePage是无效功能。所以你不能使用带有价值的回报。下面是我可以假设您的代码应该是的代码。
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")
}
}