第十一章 http标准库和其他标准库

其实这一章的内容,我们在之前的测试章节都已经涉及过了.

 

一. 模拟一个http服务端

package main

import "net/http"

type handler int

func (h *handler) ServeHTTP(writer http.ResponseWriter,request *http.Request) {
    writer.Write([]byte(abc))
}

func main() {
    h := new(handler)
    http.ListenAndServe(:8889,h)
}

这就模拟了一个服务端,我们可以网客户端发各种各样的数据. 

 

二. 模拟一个http客户端

package main

import (
    fmt"
    io/IoUtil
)

func main() {
    resp,err := http.Get(http://localhost:8889)
    if err != nil {
        panic(error)
    }

    defer resp.Body.Close()

    body,err := IoUtil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

这样就把刚刚服务端发送的abc读取出来了

 

三. 发送带有header的http请求


)

func main() {
    // 使用自定义的request
    request,err := http.NewRequest(http.MethodGet,)
    }
     添加一个ua,这样服务端吧ua取出来,就可以展示出来了
    request.Header.Add(ua",1)">)
    resp,1)"> http.DefaultClient.Do(request)
    resp,err := http.Get("http://localhost:8889")
    (body))
}

四. 第四个讲的是pprof,我之前在测试的时候已经详细研究过pprof用来监控web服务的性能,这里就不在描述了,

给出一个连接: https://www.cnblogs.com/ITPower/articles/12324659.html   

https://www.cnblogs.com/ITPower/articles/12317631.html

 

五. 其他标准库,这里也是一代而过,讲的并不详细,学完这门课,我们在集中精力研究各个标准库

相关文章

什么是Go的接口? 接口可以说是一种类型,可以粗略的理解为他...
1、Golang指针 在介绍Golang指针隐式间接引用前,先简单说下...
1、概述 1.1 Protocol buffers定义 Protocol buffe...
判断文件是否存在,需要用到"os"包中的两个函数: os.Stat(...
1、编译环境 OS :Loongnix-Server Linux release 8.3 CPU指...
1、概述 Golang是一种强类型语言,虽然在代码中经常看到i:=1...