嵌套函数不允许

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

Go nested func not allowed

问题

我想从我的数据库中获取一个表的值,并将它们解析到一个HTML中。这是我尝试做的代码,但是我得到了一个错误,错误信息是nested func not allowed

我开始创建一个结构体:

type App struct{
    Title string
    Author string
    Description string
}

我创建了一个函数来渲染模板:

func render(w http.ResponseWriter, tmpl string){
    tmpl = fmt.Sprintf("templates/%s", tmpl)
    t, err := template.ParseFiles(tmpl)
    if err != nil{
        log.Print("template parsing error: ", err)
    }
    err = t.Execute(w, "")
    if err != nil{
        log.Print("template executing error: ", err)
    }
}

然后,在这里我从数据库中获取了应用程序,并尝试将它们渲染到HTML中。

func myApps(w http.ResponseWriter, r *http.Request){
    db, err := sql.Open("postgres"," user=postgres dbname=test host=localhost password=1234 sslmode=disable")
    if err != nil{
        log.Fatal(err)
    }
    rows, err := db.Query(`SELECT title, author, description FROM apps
                             WHERE title ILIKE $1
                             OR author ILIKE $1
                             OR description ILIKE $1`)
    defer rows.Close()

    apps := []App{}
    for rows.Next(){
        b := App{}
        err := rows.Scan(&b.Title, &b.Author, &b.Description)
        apps = append(apps, b)
    }

    render(w, "myapps.html", apps)
}

提前谢谢!

英文:

I want to get the values of one table of my db and parse them in an html. This is the code that I used to try to do it but I got an error called nested func not allowed.

I started creating an estructure:

type App struct{
	Title string
	Author string
	Description string
}

I created a function to render the templates:

func render(w http.ResponseWriter, tmpl string){
	tmpl = fmt.Sprintf("templates/%s", tmpl)
	t, err := template.ParseFiles(tmpl)
	if err != nil{
		log.Print("template parsing error: ", err)
	}
	err = t.Execute(w, "")
	if err != nil{
		log.Print("template executing error: ", err)
	}
}

Then, here I got the apps from the database and try to render them to the html.

func myApps(w http.ResponseWriter, r *http.Request){
	db, err := sql.Open("postgres"," user=postgres dbname=test host=localhost password=1234 sslmode=disable")
	if err != nil{
		log.Fatal(err)
	}
		rows, err := db.Query(`SELECT title, author, description FROM apps
								 WHERE title ILIKE $1
								 OR author ILIKE $1
								 OR description ILIKE $1`)
			defer rows.Close()

			apps := []App{}
			for rows.Next(){
				b := App{}
				err := rows.Scan(&b.Title, &b.Author, &b.Description)
				apps = append(apps, b)

	render(w, "myapps.html", apps)
}

Thank you in Advance!

答案1

得分: 1

在包含for语句的行周围可能存在一些错误:

for rows.Next(){

我没有看到这个开头的'{'在其他地方关闭。

(或者它已经关闭了,但是func myApps没有关闭)

也许应该是这样的:

        for rows.Next() {
            b := App{}
            err := rows.Scan(&b.Title, &b.Author, &b.Description)
            apps = append(apps, b)
        }
        render(w, "myapps.html", apps)
}

无论如何,运行go fmt总是一个好主意。

英文:

There might be some type around the line including a for statement:

for rows.Next(){

I don't see that opening '{' closed anywhere else.

(Or it is closed, but then func myApps isn't closed)

Maybe it should be:

        for rows.Next() {
            b := App{}
            err := rows.Scan(&b.Title, &b.Author, &b.Description)
            apps = append(apps, b)
        }
        render(w, "myapps.html", apps)
}

In any case, running go fmt is always a good idea.

huangapple
  • 本文由 发表于 2014年7月10日 15:59:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/24671277.html
匿名

发表评论

匿名网友

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

确定