Go语言学习笔记(3)
action
eq 等于
ne 不等于
lt 小于 (less than)
le 小于等于
gt 大于
ge 大于等于
if else 部分
package main
import (
"html/template"
"net/http"
)
func welcome(w http.ResponseWriter, r *http.Request){
t,_:=template.ParseFiles("view/index.html")
t.Execute(w,"true")
}
func main(){
server:=http.Server{Addr:":8090"}
http.HandleFunc("/",welcome)
server.ListenAndServe()
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>action</title>
</head>
<body>
<div>get the information {{.}}</div>
<div>
{{if .}}
execute if
{{else}}
execute else
{{end }}
</div>
<div>
{{$n:=123}}
{{if gt $n 456}}
execute n>456
{{else}}
execute else n<=456
{{end}}
</div>
</body>
</html>
range部分
package main
import (
"html/template"
"net/http"
)
func welcome(w http.ResponseWriter, r *http.Request){
t,_:=template.ParseFiles("view/index.html")
arr:=[]string{"first","second"}
t.Execute(w,arr)
}
func main(){
server:=http.Server{Addr:":8090"}
http.HandleFunc("/",welcome)
server.ListenAndServe()
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>action</title>
</head>
<body>
{{range .}}
{{.}}<br/>
{{end}}
</body>
</html>
另一种方式
func welcome(w http.ResponseWriter, r *http.Request){
t,_:=template.ParseFiles("view/index.html")
m:=map[string]string{"key1":"value1","key2":"value2"}
t.Execute(w,m)
}
模板嵌套
package main
import (
"html/template"
"net/http"
)
func welcome(w http.ResponseWriter, r *http.Request){
t,_:=template.ParseFiles("view/index.html","view/head.html","view/foot.html")
t.ExecuteTemplate(w,"layout",nil)
}
func main(){
server:=http.Server{Addr:":8090"}
http.HandleFunc("/",welcome)
server.ListenAndServe()
}
{{define "head"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>head</title>
</head>
<body>
<div>head.html</div>
<div>{{.}}</div><br/>
</body>
</html>
{{end}}
{{define "foot"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>foot</title>
</head>
<body>
foot page<br/>
{{.}}<br/>
</body>
</html>
{{end}}
{{define "layout"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>templates layout</title>
</head>
<body>
<div>layout</div>
{{template "head" "contents to head"}}<br/>
<div>contents ....</div>
<br/>
{{template "foot" "contents to foot"}}<br/>
</body>
</html>
{{end}}
最后页面样式
文件上传
package main
import (
"html/template"
"io/ioutil"
"net/http"
"strings"
)
func welcome(w http.ResponseWriter, r *http.Request){
t,_:=template.ParseFiles("view/index.html")
t.Execute(w,nil)
}
func upload(w http.ResponseWriter, r *http.Request){
filename:=r.FormValue("name")
file,fileHeader,_:=r.FormFile("file")
c:=fileHeader.Filename[strings.LastIndex(fileHeader.Filename,"."):]
b,_:=ioutil.ReadAll(file)
ioutil.WriteFile("D:/"+filename+c,b,0777)
t,_:=template.ParseFiles("view/success.html")
t.Execute(w,nil)
}
func main(){
server:=http.Server{Addr:":8090"}
http.HandleFunc("/",upload)
server.ListenAndServe()
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>file upload</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
the file name<input type="text" name="name"/><br/>
the file<input type="file" name="file"/><br/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
这是success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>success</title>
</head>
<body>
file upload success!
</body>
</html>