其实这一章的内容,我们在之前的测试章节都已经涉及过了.
一. 模拟一个http服务端
package main
import @H_301_9@"@H_301_9@net/http@H_301_9@"@H_301_9@
type handler @H_301_9@int@H_301_9@
func (h @H_301_9@*handler) ServeHTTP(writer http.ResponseWriter,request *http.Request) {
writer.Write([]@H_301_9@byte@H_301_9@(abc@H_301_9@))
}
func main() {
h :@H_301_9@= new@H_301_9@(handler)
http.ListenAndServe(@H_301_9@:8889@H_301_9@,h)
}@H_301_9@
这就模拟了一个服务端,我们可以网客户端发各种各样的数据.
二. 模拟一个http客户端
package main
import (
@H_301_9@fmt@H_301_9@"@H_301_9@
io/IoUtil@H_301_9@
)
func main() {
resp,err :@H_301_9@= http.Get(http://localhost:8889@H_301_9@)
@H_301_9@if@H_301_9@ err != nil {
panic(@H_301_9@error@H_301_9@)
}
defer resp.Body.Close()
body,err :@H_301_9@= IoUtil.ReadAll(resp.Body)
fmt.Println(@H_301_9@string@H_301_9@(body))
}@H_301_9@
这样就把刚刚服务端发送的abc读取出来了
三. 发送带有header的http请求
)
func main() {
@H_301_9@//@H_301_9@ 使用自定义的request@H_301_9@
request,err := http.NewRequest(http.MethodGet,301_9@)
}
@H_301_9@ 添加一个ua,这样服务端吧ua取出来,就可以展示出来了@H_301_9@
request.Header.Add(ua@H_301_9@"@H_301_9@,1)">)
resp,1)"> http.DefaultClient.Do(request)
@H_301_9@resp,err := http.Get("@H_301_9@http://localhost@H_301_9@:8889")@H_301_9@
(body))
}@H_301_9@
四. 第四个讲的是pprof,我之前在测试的时候已经详细研究过pprof用来监控web服务的性能,这里就不在描述了,
给出一个连接: https://www.cnblogs.com/ITPower/articles/12324659.html
https://www.cnblogs.com/ITPower/articles/12317631.html
五. 其他标准库,这里也是一代而过,讲的并不详细,学完这门课,我们在集中精力研究各个标准库