gout to become the Swiss Army Knife of the http client @^^@---> gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues
gout to become the Swiss Army Knife of the http client @^^@---> gout 是http client领域的瑞士军刀,小巧,强大,犀利。具体用法可看文档,如使用迷惑或者API用得不爽都可提issues
gout 是go写的http 客户端,为提高工作效率而开发
go get github.com/guonaihong/goutexamples 目录下面的例子,都是可以直接跑的。如果觉得运行例子还是不明白用法,可以把你迷惑的地方写出来,然后提issue
cd _example
# GOPROXY 是打开go module代理,可以更快下载模块
# 第一次运行需要加GOPROXY下载模块,模块已安装的直接 go run 01-color-json.go 即可
env GOPROXY=https://goproxy.cn go run 01-color-json.goGout默认使用语言内置的encoding/json包。但是如果你想使用其他的json包,可以通过build tag来修改。
go build -tags=jsoniter .go build -tags=go_json .go build -tags="sonic avx" .…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()
}
……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
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)
}
}
// struct
type testHeader struct {
CheckIn string `header:checkin`
CheckOut string `header:checkout`
}/*
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"})数据校验使用 完成功能, 更多语法可看该文档.
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
}
}response中间件,在Bind()之前执行。可以对response进行通用逻辑处理。
如果只需要闭包逻辑,则可以使用WithResponseMiddlerFunc,而不必创建一个结构体,下面的例子中对两种方法都进行了使用。
……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)
}
}
…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)
}
}使用debug.NoColor()传入Debug函数关闭颜色高亮
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)
}
}
debug 自定义模式,可传递函数。下面演示用环境变量开启debug模式(只有传递IOS_DEBUG环境变量才输出日志)
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.godebug.Trace()可输出http各个阶段的耗时,比如dns lookup时间,tcp连接时间等等。可以很方便的做些性能调优。
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,No open issues yet, or sync has not completed.