go error:undefined:" html / template" .ParseFile

时间:2015-06-13 19:41:23

标签: go

编译代码时出现以下错误 " html / template undefined:" html / template" .ParseFile"

在源代码的字符串" t,_:= template.ParseFile(" edit.html",nil)"

package main

import (
    "net/http"      
    "io/ioutil"
    "html/template"
)

    type Page struct {
        Title string
        Body  []byte
        }

    func (p *Page) save() error {
            filename := p.Title + ".txt"
            return ioutil.WriteFile(filename, p.Body, 0600)
    }

    func loadPage(title string) (*Page, error) {
        filename := title + ".txt"
        body, err := ioutil.ReadFile(filename)
        if err != nil {
            return nil, err
        }
        return &Page{Title: title, Body: body}, nil
    }

    const lenPath = len("/view/")


    func editHandler(w http.ResponseWriter, r *http.Request) {
        title := r.URL.Path[lenPath:]
        p, err := loadPage(title)
        if err != nil {
            p = &page{title: title}
        }
        t, _ := template.ParseFile("edit.html", nil)
        t.Execute(p, w)
    }

    func main() {
        http.HandleFunc("/edit/", editHandler)
        http.ListenAndServe(":8080", nil)
    }

帮助删除此错误。

1 个答案:

答案 0 :(得分:1)

应为template.ParseFiles("edit.html")

复数不单数

http://golang.org/pkg/html/template/#ParseFiles