百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

gout

> 开发工具
开源

gout to become the Swiss Army Knife of the http client @^^@---> gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues

1.3K stars0 点赞3 次浏览
访问官网GitHub

工具介绍

gout to become the Swiss Army Knife of the http client @^^@---> gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues

gout

gout 是go写的http 客户端,为提高工作效率而开发

构架

feature

  • 支持设置 GET/PUT/DELETE/PATH/HEAD/OPTIONS
  • 支持设置请求 http header(可传 struct,map,array,slice 等类型)
  • 支持设置 URL query(可传 struct,map,array,slice,string 等类型)
  • 支持设置 json 编码到请求 body 里面(可传struct, map, string, []byte 等类型)
  • 支持设置 xml 编码到请求 body 里面(可传struct, string, []byte 等类型)
  • 支持设置 yaml 编码到请求 body 里面(可传struct, map, string, []byte 等类型)
  • 支持设置 protobuf 编码到请求 body里面(可传struct)
  • 支持设置 form-data(可传 struct, map, array, slice 等类型)
  • 支持设置 x-www-form-urlencoded(可传 struct,map,array,slice 等类型)
  • 支持设置 io.Reader,uint/uint8/uint16...int/int8...string...[]byte...float32,float64 至请求 body 里面
  • 支持解析响应body里面的json,xml,yaml至结构体里(BindJSON/BindXML/BindYAML)
  • 支持解析响应body的内容至io.Writer, uint/uint8...int/int8...string...[]byte...float32,float64
  • 支持解析响应header至结构体里
  • 支持接口性能benchmark,可控制压测一定次数还是时间,可控制压测频率
  • 支持retry-backoff,可以指定重试条件
  • 支持发送裸http数据包
  • 支持导出curl命令
  • 传入自定义*http.Client
  • 支持请求中间件()
  • 支持响应中间件ResponseUse
  • 支持设置chunked数据格式发送
  • 支持body, header的数据校验
  • 支持通过build tag自由选择不同的json序列化方式(可选jsoniter,go_json,sonic等)
  • 等等更多

演示

内容

  • Installation

  • example

  • quick start

  • API Examples

    • GET POST PUT DELETE PATH HEAD OPTIONS
      • GET POST PUT DELETE PATH HEAD OPTIONS template
    • Query Parameters
    • http header *Do not convert http headers
      • Set request header *Parsing the response header
      • get all header
      • data valid
      • responseUse
    • http body
      • body
        • Set the data to the http request body
        • Parse the response body into a variable
      • json
        • Serialize json to request body
        • Parsed http response body in json format
        • Do not escape html characters
      • yaml
      • xml
      • form-data
      • x-www-form-urlencoded
        • protobuf
      • callback
        • get *http.Response
        • multiple binding functions
        • Auto decode body
      • Set request timeout
    • proxy
      • socks5
    • cookie
      • basic auth
    • context
      • Cancel a sending request
    • unix socket
    • http2 doc
    • debug mode
      • Turn on debug mode
      • Turn off color highlighting in debug mode
        • Custom debug mode
        • trace info
        • save to writer
        • save to file
        • Extracting trace information
      • benchmark
        • benchmarking a certain number of times
        • benchmarking for a certain time
        • benchmarking at a fixed frequency
        • Custom benchmark functions
      • retry backoff
        • Specify the retry conditions, when equal to a certain http code
        • Specify retry conditions. The default URL cannot be accessed. Use the backup URL
      • import
        • send raw http request
      • export
        • generate curl command
        • generate curl command and send HTTP request
      • Incoming custom * http.Client
      • Using chunked data format
      • NewWithOpt
        • Insecure skip verify
        • Turn off 3xx status code automatic jump
        • NewWithOpt set timeout
        • NewWithOpt unix sock
      • NewWithOpt proxy
        • NewWithOpt socks5
      • Global configuration
        • set timeout
        • set debug
  • Unique features

    • forward gin data
  • FAQ

Installation

go get github.com/guonaihong/gout

example

examples 目录下面的例子,都是可以直接跑的。如果觉得运行例子还是不明白用法,可以把你迷惑的地方写出来,然后提issue

运行命令如下

bash
cd _example
# GOPROXY 是打开go module代理,可以更快下载模块
# 第一次运行需要加GOPROXY下载模块,模块已安装的直接 go run 01-color-json.go 即可
env GOPROXY=https://goproxy.cn go run 01-color-json.go

build tag

Gout默认使用语言内置的encoding/json包。但是如果你想使用其他的json包,可以通过build tag来修改。

jsoniter

bash
go build -tags=jsoniter .

go-json

bash
go build -tags=go_json .

sonic

bash
go build -tags="sonic avx" .

quick start

…

返回

API examples

GET POST PUT DELETE PATH HEAD OPTIONS

go
package main

import (
 "github.com/guonaihong/gout"
)

func main() {
 url := "https://github.com"
 // 发送GET方法
 gout.GET(url).Do()

 // 发送POST方法
 gout.POST(url).Do()

 // 发送PUT方法
 gout.PUT(url).Do()

 // 发送DELETE方法
 gout.DELETE(url).Do()

 // 发送PATH方法
 gout.PATCH(url).Do()

 // 发送HEAD方法
 gout.HEAD(url).Do()

 // 发送OPTIONS
 gout.OPTIONS(url).Do()
}

返回

GET POST PUT DELETE PATH HEAD OPTIONS template

…

返回

Query Parameters

SetQuery

…

go package main

import ( "fmt" "github.com/guonaihong/gout" "time" )

func main() { err := gout. //设置GET请求和url,:8080/test.header是127.0.0.1:8080/test.header的简写 GET(":8080/test.header"). //设置debug模式 Debug(true). //设置请求http header SetHeaderRaw(gout.H{ "h1": "v1", "h2": 2, "h3": float32(3.14), "h4": 4.56, "h5": time.Now().Unix(), "h6": time.Now().UnixNano(), "h7": time.Now().Format("2006-01-02")}). Do() if err != nil { fmt.Printf("%s\n", err) return }

}

/*

GET /test.header HTTP/1.1 h2: 2 h3: 3.14 h4: 4.56 h5: 1574081686 h6: 1574081686471347098 h7: 2019-11-18 h1: v1

GET /test.header HTTP/1.1

H2: 2 H3: 3.14 H4: 4.56 H5: 1574081686 H6: 1574081686471347098 H7: 2019-11-18 H1: v1

GET /test.header HTTP/1.1

go
package main

import (
    "fmt"
    "github.com/guonaihong/gout"
)

type testHeader struct {
    CheckIn  string `header:checkin`
    CheckOut string `header:checkout`
}

func main() {

    t := testHeader{}

    code := 0

    err := gout.
        GET(":8080/testquery").
        Code(&code).
        SetHeader( /*看下面支持的类型*/ ).
        BindHeader(&t).
        Do()
    if err != nil {
        fmt.Printf("fail:%s\n", err)
    }   
}
  • BindHeader支持的类型有 结构体
go
// struct
type testHeader struct {
    CheckIn string `header:checkin`
    CheckOut string `header:checkout`
}
  • SetHeader支持的类型有
go
/*
map[string]interface{},可以使用gout.H别名
struct
array, slice(长度必须是偶数)
*/

// gout.H 或者 map[string]interface{}
SetHeader(gout.H{
    "check_in":"2019-06-18",
    "check_out":"2019-06-18",
})

// struct
type testHeader struct {
    CheckIn string `header:checkin`
    CheckOut string `header:checkout`
}

SetHeader(&testHeader{CheckIn:2019-06-18, CheckOut:2019-06-18})

// array or slice
// -H active:enable -H action:drop
SetHeader([]string{"active", "enable", "action", "drop"})

返回

data valid

数据校验使用 完成功能, 更多语法可看该文档.

go
import (
 "fmt"
 "github.com/guonaihong/gout"
)

type testValid struct {        
        Val string `valid:"required"`   
} 

func main() {
 tv := testValid{}
 err := gout.
  // 设置POST方法和url
  POST(":8080/req/body").
  //打开debug模式
  Debug(true).
  //解析json, 并且当需要的字段没有值时, 返回错误
  BindJSON(&tv).
  //结束函数
  Do()

 if err != nil {
  fmt.Printf("%s\n", err)
  return
 }

}

返回

responseUse

response中间件,在Bind()之前执行。可以对response进行通用逻辑处理。 如果只需要闭包逻辑,则可以使用WithResponseMiddlerFunc,而不必创建一个结构体,下面的例子中对两种方法都进行了使用。

…

返回

http body

body

Set the data to the http request body

…

返回

Parsed http response body in json format

go
package main

import (
 "fmt"
 "github.com/guonaihong/gout"
)

type rsp struct {
 ErrMsg  string `json:"errmsg"`
 ErrCode int    `json:"errcode"`
}

func main() {
 rsp := rsp{}
 err := gout.
  GET(":8080/colorjson").
  //打开debug模式
  Debug(true).
  //绑定响应json数据到结构体
  BindJSON(&rsp).
  //结束函数
  Do()

 if err != nil {
  fmt.Printf("err = %v\n", err)
 }
}

返回

do not escape html characters

  • SetJSONNotEscape 和SetJSON唯一的区别就是不转义HTML字符
…

go package main

import ( "fmt" "github.com/guonaihong/gout" )

func main() { s := "" err := gout. GET("https://http2.golang.org/reqinfo"). //设置GET请求和请求url Debug(true). //打开debug模式,可以看到请求数据和响应数据 SetBody("hello, ###########"). //设置请求body的内容,如果你的请求内容是json格式,需要使用SetJSON函数 BindBody(&s). //解析响应body内容 Do() //结束函数

if err != nil {
    fmt.Printf("send fail:%s\n", err)
}   
_ = s 

}


[返回](#内容)

## debug mode

### Turn on debug mode

该模式主要方便调试用的,默认开启颜色高亮(如果要关闭颜色高亮,请往下看)

```go
func main() {
 
 err := gout.POST(":8080/colorjson").
  Debug(true). //打开debug模式
  SetJSON(gout.H{"str": "foo",
   "num":   100,
   "bool":  false,
   "null":  nil,
   "array": gout.A{"foo", "bar", "baz"},
   "obj":   gout.H{"a": 1, "b": 2},
  }).Do()

 if err != nil {
  fmt.Printf("err = %v\n", err)
 }
}

返回

Turn off color highlighting in debug mode

使用debug.NoColor()传入Debug函数关闭颜色高亮

go
import (
 "github.com/guonaihong/gout"
 "github.com/guonaihong/gout/debug"
)
func main() {
 
 err := gout.POST(":8080/colorjson").
  Debug(debug.NoColor()).
  SetJSON(gout.H{"str": "foo",
   "num":   100,
   "bool":  false,
   "null":  nil,
   "array": gout.A{"foo", "bar", "baz"},
   "obj":   gout.H{"a": 1, "b": 2},
  }).Do()

 if err != nil {
  fmt.Printf("err = %v\n", err)
 }
}

返回

Custom debug mode

debug 自定义模式,可传递函数。下面演示用环境变量开启debug模式(只有传递IOS_DEBUG环境变量才输出日志)

go
package main

import (
    "fmt"
    "github.com/guonaihong/gout"
 "github.com/guonaihong/gout/debug"
    "os"
)

func IOSDebug() debug.Apply {
    return gout.DebugFunc(func(o *debug.Options) {
        if len(os.Getenv("IOS_DEBUG")) > 0 { 
            o.Debug = true
        }
    })  
}

func main() {

    s := ""
    err := gout.
        GET("127.0.0.1:8080").
        // Debug可以支持自定义方法
        // 可以实现设置某个环境变量才输出debug信息
        // 或者debug信息保存到文件里面,可以看下_example/15-debug-save-file.go
        Debug(IOSDebug()).
        SetBody("test hello").
        BindBody(&s).
        Do()

    fmt.Printf("err = %v\n", err)
}

// env IOS_DEBUG=true go run customize.go

返回

trace info

debug.Trace()可输出http各个阶段的耗时,比如dns lookup时间,tcp连接时间等等。可以很方便的做些性能调优。

go
package main

import (
    "fmt"
    "github.com/guonaihong/gout"
 "github.com/guonaihong/gout/debug"
)

func openDebugTrace() {
    err := gout.POST(":8080/colorjson").
        Debug(debug.Trace()).
        SetJSON(gout.H{"str": "foo",
            "num":   100,
            "bool":  false,
            "null":  nil,

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Gogogolanghttp-clienthttp-request

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类开发工具
定价开源

> 相关工具

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具