DB和上下文太多错误。

huangapple go评论83阅读模式
英文:

DB & context too many errors

问题

我正在运行以下"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中的说明,所有的值都被获取了,但是我不确定为什么会出现未定义变量的问题。我认为这些变量不需要被创建,但如果需要创建,请告诉我。

谢谢。

英文:

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\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")
}
}

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

得分: 2

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

homePage是一个无返回值的函数,所以你不能将它用作函数参数。

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

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

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

如我在上面所说,homePage是一个无返回值的函数,所以你不能使用带有返回值的return语句。下面是我猜测你的代码应该是这样的:

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")
	}
}
英文:

> ./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\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")
}
}

huangapple
  • 本文由 发表于 2017年7月5日 07:33:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/44915201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定