package main
import (
"fmt"
"log"
"net/http"
"os"
"text/template"
)
type Person struct {
Name string
age string
}
func GetTest(w http.ResponseWriter, r *http.Request){
t, err := template.ParseFiles("F:\\go_test\\src\\10_http\\index.html")
if err != nil {
fmt.Println("parse file err:", err)
return
}
p := Person{Name: "Mary", age: "31"}
file, err := os.OpenFile("testxxx.txt", os.O_CREATE|os.O_RDONLY, 0755)
defer file.Close()
if err != nil{
}
if err := t.Execute(file, p); err != nil {
fmt.Println("There was an error:", err.Error())
}
}
func main() {
fmt.Println("listening...")
http.HandleFunc("/test", GetTest)
http.HandleFunc("/test1", logPanics(SimpleServer))
http.HandleFunc("/test2", logPanics(FormServer))
if err := http.ListenAndServe(":8088", nil); err != nil {
fmt.Println("listen error")
}
}
func logPanics(handle http.HandlerFunc) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
defer func() {
if x := recover(); x != nil {
log.Printf("[%v] caught panic: %v", request.RemoteAddr, x)
}
}()
handle(writer, request)
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{.Name}}hello!{{.}}
</body>
</html>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步