代码拉取完成,页面将自动刷新
package gotool
import (
"bytes"
"net/http"
"testing"
)
type shouldBindJSONStruct struct {
Z string `json:"z"`
Both string `json:"both"`
}
type shouldBindQueryBase struct {
A string `json:"a"`
B string `json:"b"`
}
type shouldBindQueryStruct struct {
*shouldBindQueryBase
Z string `json:"z"`
Both string `json:"both"`
Num int `json:"num"`
F float64 `json:"f"`
}
// TestShouldBindJSON json入参绑定
func TestShouldBindJSON(t *testing.T) {
r, err := http.NewRequest("POST", "/", bytes.NewBuffer([]byte(`{"z":"z","both":"both"}`)))
if err != nil {
t.Fatal(err)
}
data := new(shouldBindJSONStruct)
err = ShouldBindJson(r, data)
if err != nil {
t.Fatal(err)
}
t.Log(data)
}
// TestShouldBindQuery query入参绑定
func TestShouldBindQuery(t *testing.T) {
r, err := http.NewRequest("GET", "/test?a=a&b=b&z=z&both=both&num=1698205401000&f=100000.121", bytes.NewBuffer([]byte(`{"z":"z","both":"both"}`)))
if err != nil {
t.Fatal(err)
}
data := new(shouldBindQueryStruct)
err = ShouldBindQuery(r, data)
if err != nil {
t.Fatal(err)
}
t.Log(data)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。