Ai
1 Star 0 Fork 0

叶明志/golang练习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
yemingzhi 提交于 2019-07-27 22:51 +08:00 . html里面使用数组
package main
import(
"fmt"
"net/http"
"text/template"
// "os"
)
var myTemplate *template.Template
type Person struct{
Name string
Title string
Age int
}
type Result struct{
output string
}
func (p *Result)Write(b []byte) (n int, err error){
fmt.Println("result is writing")
p.output+=string(b)
return len(b),nil
}
func userInfo(w http.ResponseWriter,r *http.Request){
fmt.Println("handle userInfo")
// fmt.Fprintf(w,"userInfo")
var arr []Person
p:=Person{Name:"Mary",Age:56,Title:"我的个人网站",}
p1:=Person{Name:"Mary1",Age:56,Title:"我的个人网站",}
p2:=Person{Name:"Mary2",Age:56,Title:"我的个人网站",}
p3:=Person{Name:"Mary3",Age:56,Title:"我的个人网站",}
arr=append(arr,p)
arr=append(arr,p1)
arr=append(arr,p2)
arr=append(arr,p3)
err:=myTemplate.Execute(w,arr)
if err!=nil{
fmt.Println(err)
}
// myTemplate.Execute(os.Stdout,p)
// file,err:=os.OpenFile("/home/huyezhou/go/src/test.log",os.O_CREATE|os.O_WRONLY,0755)
// if err!=nil{
// fmt.Println("open failed err:",err)
// return
// }
// myTemplate.Execute(file,p)
// resultWriter:=&Result{}
// myTemplate.Execute(resultWriter,p)
// fmt.Println("template render data:",resultWriter.output)
}
func initTemplate(filename string)(err error){
myTemplate,err=template.ParseFiles(filename)
if err!=nil{
fmt.Println("parse file error:",err)
return
}
return
}
func main(){
initTemplate("./index.html")
http.HandleFunc("/user/Info",userInfo)
err:=http.ListenAndServe("0.0.0.0:8080",nil)
if err!=nil{
fmt.Println("http listen failed")
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yemingzhi/GolangLearnPractice1.git
git@gitee.com:yemingzhi/GolangLearnPractice1.git
yemingzhi
GolangLearnPractice1
golang练习
2bf136849dce

搜索帮助