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