gpt4 book ai didi

转到错误 : undefined: "html/template". ParseFile

转载 作者:数据小太阳 更新时间:2023-10-29 03:41:29 24 4
gpt4 key购买 nike

<分区>

编译代码时出现如下错误"html/模板未定义:"html/模板".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)
}

帮助 tp 消除这个错误。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com