http请求
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"net/http"
)
func aa(w http.ResponseWriter, r *http.Request) {
//写入到浏览器中
fmt.Fprintln(w, "我是abc的路径")
}
func main() {
//第一个参数是路径 第二个参数是方法
http.HandleFunc("/abc", aa)
//监听端口
http.ListenAndServe("localhost:8080", nil)
}
多函数路径访问
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"net/http"
)
func aa(w http.ResponseWriter, r *http.Request) {
//写入到浏览器中
fmt.Fprintln(w, "我是aa的方法")
}
func bb(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "我是bb的方法")
}
func main() {
//创建服务器地址
server := http.Server{Addr: "localhost:8080"}
//把方法和路径绑定上
http.HandleFunc("/aa", aa)
http.HandleFunc("/bb", bb)
//监听服务器
server.ListenAndServe()
}
获取请求头和请求参数
package main
import (
"fmt"
"net/http"
)
func aa(w http.ResponseWriter, r *http.Request) {
h := r.Header
//获取header头的内容
fmt.Fprintln(w, h)
fmt.Fprintln(w, "-----------------------")
//先解析参数
r.ParseForm()
//这里才能获取浏览器参数
fmt.Fprintln(w, r.Form)
fmt.Fprintln(w, "----------------------")
//获取name的值
fmt.Fprintln(w, r.FormValue("name"))
}
func main() {
server := http.Server{Addr: "localhost:8080"}
server.Handler = http.HandlerFunc(aa)
server.ListenAndServe()
}
http://localhost:8080/aa?name=aa&pwd=666
访问html静态资源
test.go
package main
import (
"html/template"
"net/http"
)
func aa(w http.ResponseWriter, r *http.Request) {
//解析html文件
t, _ := template.ParseFiles("view/index.html")
t.Execute(w, nil)
}
func main() {
//服务器端口
server := http.Server{
Addr: "localhost:8080",
}
//设置静态文件被访问
//当url发现以static开头的 就吧请求转发到指定的路径
//http://localhost:8080/static/js/index.js
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
//访问路径
http.HandleFunc("/", aa)
//监听服务器
server.ListenAndServe()
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="/static/js/index.js"></script>
</head>
<body>
这是index首页
<button onclick="danJi()">点我</button>
</body>
</html>
index.js
function danJi(){
alert("点击了")
}
动态模版传参
test.go
package main
import (
"html/template"
"net/http"
)
// 创建结构体
type User struct {
//字段必须大写 否则html不显示
Name string
Pwd string
}
func aa(w http.ResponseWriter, r *http.Request) {
//解析html文件
t, _ := template.ParseFiles("view/index.html")
//把对象放入
t.Execute(w, User{"张三", "123"})
}
func main() {
//服务器端口
server := http.Server{
Addr: "localhost:8080",
}
//设置静态文件被访问
//当url发现以static开头的 就吧请求转发到指定的路径
//http://localhost:8080/static/js/index.js
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
//访问路径
http.HandleFunc("/", aa)
//监听服务器
server.ListenAndServe()
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="/static/js/index.js"></script>
</head>
<body>
动态显示内容:{{.Name}}---{{.Pwd}}
</body>
</html>
在模版中调用函数
test.go
package main
import (
"html/template"
"net/http"
"time"
)
func geShiHua(t time.Time) string {
//格式化时间 固定格式
return t.Format("2006-01-02 15:04:05")
}
func aa(w http.ResponseWriter, r *http.Request) {
fm := template.FuncMap{"shiJian": geShiHua}
t := template.New("index.html").Funcs(fm)
//解析html文件
t, _ = t.ParseFiles("view/index.html")
//年月日 时分秒 纳秒
time2 := time.Date(2024, 2, 22, 13, 55, 40, 0, time.Local)
//把对象放入
t.Execute(w, time2)
}
func main() {
//服务器端口
server := http.Server{
Addr: "localhost:8080",
}
//设置静态文件被访问
//当url发现以static开头的 就吧请求转发到指定的路径
//http://localhost:8080/static/js/index.js
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
//访问路径
http.HandleFunc("/", aa)
//监听服务器
server.ListenAndServe()
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="/static/js/index.js"></script>
</head>
<body>
动态显示内容,这里是空格 点:{{shiJian .}}
</body>
</html>
if else 模版
test.go
package main
import (
"html/template"
"net/http"
)
func bb(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("view/index.html")
t.Execute(w, "123")
}
func main() {
//服务器端口
server := http.Server{
Addr: "localhost:8080",
}
//访问路径
http.HandleFunc("/", bb)
//监听服务器
server.ListenAndServe()
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="/static/js/index.js"></script>
</head>
<body>
{{if .}}
显示内容:{{.}}
{{else}}
没有匹配
{{end}}
</body>
</html>
如果有值 就显示
range 循环展示内容
test.go
package main
import (
"html/template"
"net/http"
)
func bb(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("view/index.html")
//切片数组
str := []string{"aa", "bb", "cc"}
t.Execute(w, str)
}
func main() {
//服务器端口
server := http.Server{
Addr: "localhost:8080",
}
//访问路径
http.HandleFunc("/", bb)
//监听服务器
server.ListenAndServe()
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="/static/js/index.js"></script>
</head>
<body>
{{range .}}
{{.}}
{{end}}
</body>
</html>
模版嵌套
test.go
package main
import (
"html/template"
"net/http"
)
func bb(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("view/index.html", "view/aa.html")
t.ExecuteTemplate(w, "index", nil)
}
func main() {
//服务器端口
server := http.Server{
Addr: "localhost:8080",
}
//访问路径
http.HandleFunc("/", bb)
//监听服务器
server.ListenAndServe()
}
index.html
{{define "index"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="/static/js/index.js"></script>
</head>
<body>
我是index界面<hr/>
{{template "aa" "给aa界面传参数"}}
</body>
</html>
{{end}}
aa.html
{{define "aa"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
我是aa界面,获取对方传过来的参数:{{.}}
</body>
</html>
{{end}}