golang之http笔记(更新中)

127 阅读4分钟

html网页交互

开始监听

http.HandleFunc("/", Hello)
//http监听端口,一旦有"127.0.0.1/"请求过来以后,直接跳转到Hello
http.HandleFunc("/user/login", Login)
//http监听端口,一旦有"127.0.0.1/user/login"请求过来以后,直接跳转到Login


err := http.ListenAndServe("0.0.0.0:8880", nil)
//开启http端口监听,必须写在后面
if err != nil {
    fmt.Println("http listen err:", err)
    return
}

发送/接收内容

//ResponseWriter发送给用户,Request接收来自用户的信息
func Hello(res http.ResponseWriter, req *http.Request) {
    //打印给终端
    fmt.Println("hello world hello http")
    //发送给网页,res=http.ResponseWriter就是写给网页,在网页上显示
    fmt.Fprintf(res, "Say hi")
    //和上面方法一样,也是写给网页
    io.WriteString(res, "<h1>test1 SimpleServer</h1>")
}

发送/接收内容,根据方法回复

//网页文件,里面包含post
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>`

//http.ResponseWriter发给网页,http.Request接收内容
func FormServer(res http.ResponseWriter, request *http.Request) {
	//设置头文件,告诉浏览器自己是个html文件,回写给服务器
	res.Header().Set("Content-Type", "text/html")//去掉后就没乱码了,好像加不加无影响
    //接收网页返回的方法
	switch request.Method {
	//如果收到get请求
	case "GET":
        //在这个网页点按钮后回返回post
		io.WriteString(res, form)
	//如果收到post请求,我写的代码发给我的内容,表示用户按下按钮
	case "POST":
		request.ParseForm()
		//返回in的第一个内容,<br/>换行
		io.WriteString(res, request.Form["in"][0]+"<br/>")
		//返回in的第一个内容
		io.WriteString(res, request.Form["in"][1])
		fmt.Println("post")
	}
}

http请求方法

//get无安全性 post大小不受限制
//put用于在服务器上创建资源
//Head请求同步信息 delete
//http客户端配置

http请求常见状态/头部信息

//http.StatusOK = 200,如果表示200就表示成功
//http.StatusNotFound = 404,表示请求页面不存在
//http.StatusInternalServerError = 500,服务器内部错误
//http.StatusForbidden = 403,表示禁止访问

//读取头部信息
req, err := http.Head(v)

//输出头部信息
fmt.Printf("head succ.status:%v\n", req.Status)

读取头部信息,设置拨号优化,等待时间

//定义URL数组
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>

备注

<!--"abcd"-->

网页头部信息

<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}}