当前位置:网站首页>Go: send the get request and parse the return JSON (go1.16.4)
Go: send the get request and parse the return JSON (go1.16.4)
2022-07-03 13:48:00 【Lao Liu, you are so awesome】
One ,get The way of php Code and return :
Code :
res.php
<?php
$data = [
'code'=>0,
'msg'=>'success',
'data'=>$_GET
];
echo json_encode($data);
exit;
?>
return :
explain : Liu Hongdi's go The forest is a focus golang The blog of ,
Address :https://blog.csdn.net/weixin_43881017
explain : author : Liu Hongdi mailbox : [email protected]
Two ,go Code :
testController.go
// User information info
func (u *TestController) Get(c *gin.Context) {
// obtain url, Initiate request
url := "http://127.0.0.1/res.php?username=laoliu"
req, _ := http.NewRequest("GET", url, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
// Get the result back
body, _ := ioutil.ReadAll(res.Body)
bodystr := string(body)
// For returned json Analyze the data
var dataAttr map[string]interface{}
var code string
var username string
if err := json.Unmarshal([]byte(bodystr), &dataAttr); err == nil {
for idx, value := range dataAttr {
fmt.Println(idx)
fmt.Println(value)
if (idx == "code") {
code = fmt.Sprintf("%v", value)
}
if (idx == "data") {
mapTmp := value.(map[string]interface{})
username = mapTmp["username"].(string)
}
}
} else {
fmt.Println("json Parsing error :");
fmt.Println(err.Error());
}
// return
m := map[string]interface{} {
"resCode":code,
"username":username,
}
resp := result.NewResult(c)
resp.Success(m)
}
return :
3、 ... and , see go Version of :
[email protected]:~# go version
go version go1.16.4 linux/amd64
边栏推荐
- 服务器硬盘冷迁移后网卡无法启动问题
- 全面发展数字经济主航道 和数集团积极推动UTONMOS数藏市场
- 掌握Cypress命令行选项,是真正掌握Cypress的基础
- 又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
- Red Hat Satellite 6:更好地管理服务器和云
- 【556. 下一个更大元素 III】
- Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
- Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
- RichView TRVStyle ListStyle 列表样式(项目符号编号)
- Error running 'application' in idea running: the solution of command line is too long
猜你喜欢
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
Go language unit test 3: go language uses gocovey library to do unit test
Flutter动态化 | Fair 2.5.0 新版本特性
Mycms we media mall v3.4.1 release, user manual update
Internet of things completion -- (stm32f407 connects to cloud platform detection data)
Go language unit test 5: go language uses go sqlmock and Gorm to do database query mock
Multi table query of MySQL - multi table relationship and related exercises
Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
Mastering the cypress command line options is the basis for truly mastering cypress
随机推荐
Windos creates Cordova prompt because running scripts is prohibited on this system
研发团队资源成本优化实践
顺序表(C语言实现)
Ubuntu 14.04 下开启PHP错误提示
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
【556. 下一个更大元素 III】
树的深入和广度优先遍历(不考虑二叉树)
Unity EmbeddedBrowser浏览器插件事件通讯
Anan's doubts
Which securities company has the lowest Commission for opening an account online? I want to open an account. Is it safe for the online account manager to open an account
[sort] bucket sort
Software testing is so hard to find, only outsourcing offers, should I go?
Students who do not understand the code can also send their own token, which is easy to learn BSC
软件测试工作那么难找,只有外包offer,我该去么?
Red Hat Satellite 6:更好地管理服务器和云
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
Record 405 questions about bank callback post request
JSON serialization case summary
掌握Cypress命令行选项,是真正掌握Cypress的基础