html网页交互
开始监听
http.HandleFunc("/", Hello)
http.HandleFunc("/user/login", Login)
err := http.ListenAndServe("0.0.0.0:8880", nil)
if err != nil {
fmt.Println("http listen err:", err)
return
}
发送/接收内容
func Hello(res http.ResponseWriter, req *http.Request) {
fmt.Println("hello world hello http")
fmt.Fprintf(res, "Say hi")
io.WriteString(res, "<h1>test1 SimpleServer</h1>")
}
发送/接收内容,根据方法回复
const form = `<html>
<body>
<form action"#" method="post" name="tag">
<label>文本:</label><input type="test" name="in"/>
<br />
<label>密码:</label><input type="password" name="in"/>
<br />
<input type="submit" value="submit">
</form>
</body>
</html>`
func FormServer(res http.ResponseWriter, request *http.Request) {
res.Header().Set("Content-Type", "text/html")
switch request.Method {
case "GET":
io.WriteString(res, form)
case "POST":
request.ParseForm()
io.WriteString(res, request.Form["in"][0]+"<br/>")
io.WriteString(res, request.Form["in"][1])
fmt.Println("post")
}
}
http请求方法
http请求常见状态/头部信息
req, err := http.Head(v)
fmt.Printf("head succ.status:%v\n", req.Status)
读取头部信息,设置拨号优化,等待时间
var url = []string{
"http://www.taobao.com",
"http://www.baidu.com",
"http://www.google.com",
}
func main() {
for _, v := range url {
client := http.Client{
Transport: &http.Transport{
Dial: func(network, addr string) (net.Conn, error) {
timeout := time.Second * 2
return net.DialTimeout(network, addr, timeout)
},
},
}
req, err := client.Head(v)
if err != nil {
fmt.Printf("head:%s,err:%v\n", v, err)
return
}
fmt.Printf("head succ.status:%v\n", req.Status)
}
}
标签
myTemplate, err = myTemplate.Execute(res, "index.html")
person := Person{
Name: "张三",
Age: 18,
Title: "个人网站",
}
myTemplate.Execute(res, person)
html简单写法
基础
<html></html>
备注
网页头部信息
<head><title></title></head>
网页抬头,修改网页标签信息
例:
<head>
<title>abcd</title>
</head>
网页内容
最外嵌套body
<body></body>
例:
<body>
<h1>abcd</h1>
</body>
表格table
<table></table>
最外嵌套
<table border="1">
border表示表格最外圈框的厚度,改成0就看不出来最外框了
<tr></tr>
表示表格的一行
<td></td>
例:
<td colspan="2" text-align="centor">
班级信息
</td>
colspan表示占用几个格子长度
<td>
<a href="http://www.baidu.com" target="blank">
点击领取大礼包
</a>
</td>
colspan表示占用几个格子长度
例:
<table border="1">
<tr>
<td colspan="2" text-align="centor">总信息</td>
</tr>
<tr>
<td>很好</td><td>很帅</td>
</tr>
<tr>
<td>工坊</td><td>好玩</td>
</tr>
<td>酷</td>
<td>
<a href="http://www.baidu.com" target="blank">
点击领取大礼包
</a>
</td>
</table>
标题h1
<h1></h1>
标题,h1最大,h6最小的
例:
<h1>abcd</h1>
加一行横杠hr
<hr />
-----------------------------------------
换行br
<br />
段落标签p
<p></p>
在这行文本结束后会有一个空行
例:
<p>abcde</p>
<p>abcde</p>
结果:
abcde
abcde
表单form
<form></form>
表单,里面有输入框,有按钮
<form action"#" method="post" name="tag">
method指返回的信号是"post"POST,name是名字,可以不加
例:
<form action"#" method="post" name="tag">
<label>文本:</label><input type="test" name="in"/>
<br />
<label>密码:</label><input type="password" name="in"/>
<br />
<label>单选:</label><input type="radio" name="gender">男<input type="radio" name="gender">女
<br />
<label>多选:</label><input type="checkbox" name="gender">足球<input type="checkbox" name="gender">篮球
<br />
<label>下拉菜单:</label>
<select>
<option>淘宝</option>
<option>京东</option>
</select>
<br />
<input type="submit" value="submit">
</form>
输入框input
<input type="类型">
类型:
·text 文本
·password 密码,输入的时候会显示*******
·radio 单选,二选一
例:
<label>单选:</label><input type="radio" name="gender">男<input type="radio" name="gender">女
·checkbox 多选
例:
<label>多选:</label><input type="checkbox" name="gender">足球<input type="checkbox" name="gender">篮球
下拉菜单:
<label>下拉菜单:</label>
<select>
<option>淘宝</option>
<option>京东</option>
</select>
<input type="类型" name="数组名"/>
name表示会传给服务器数组名,用下标表示
["数组名"][0]
request.Form["in"][0]
例:
<input type="test">
输入框前文本label
<label></label>
例:
<label>姓名:</label><input type="test">
按钮input
<input type="submit" value="submit">
按下按钮会会把input里的内容传给服务器,type是按钮类型value是按钮名字
无序列表ur
<ur></ur>
例:
<ur>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
</ur>
结果:
· a
· b
· c
· d
有序列表ol
<ol></ol>
例:
<ol>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
</ol>
结果:
1.a
2.b
3.c
4.d
判断
{{if gt .Age 30}}
<h1>老羊肉</h1>
<h2>姓名:{{.Name}}</h2>
<h2>年龄:{{.Age}}</h2>
{{else}}
<h1>小鲜肉</h1>
<h2>姓名:{{.Name}}</h2>
<h2>年龄:{{.Age}}</h2>
{{end}}
gt大于
lt小于
eq等于
gte大于等于
lte小于等于
not非
{{if not .condition1 .condition2}}
and与
or或
{{if and .condition1 .condition2}}
复用变量
with指定当前变量,.代表当前变量
{{with .Name}}
<h2>hello {{.}}</h2>
{{end}}
遍历
对传过来的数组进行遍历.代表传过来的值
{{range .}}
<tr>
<td>{{.Name}}</td><td>{{.Age}}</td><td>{{.Title}}</td>
</tr>
{{end}}
或者
{{range .Name}}
<tr>
<td>{{.Name}}</td><td>{{.Age}}</td><td>{{.Title}}</td>
</tr>
{{end}}