当前位置:网站首页>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边栏推荐
猜你喜欢

Flutter动态化 | Fair 2.5.0 新版本特性

SQL Injection (GET/Search)

Richview trvstyle liststyle list style (bullet number)

3D视觉——2.人体姿态估计(Pose Estimation)入门——OpenPose含安装、编译、使用(单帧、实时视频)

挡不住了,国产芯片再度突进,部分环节已进到4nm

106. How to improve the readability of SAP ui5 application routing URL

CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team

【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】

Halcon combined with C # to detect surface defects -- Halcon routine autobahn

刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
随机推荐
Kivy tutorial how to automatically load kV files
Go language web development series 28: solve cross domain access of CORS with gin contrib / CORS
Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.
Realize the recognition and training of CNN images, and process the cifar10 data set and other methods through the tensorflow framework
Multi table query of MySQL - multi table relationship and related exercises
Ubuntu 14.04 下开启PHP错误提示
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
MySQL installation, uninstallation, initial password setting and general commands of Linux
Flutter dynamic | fair 2.5.0 new version features
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
json序列化时案例总结
【被动收入如何挣个一百万】
User and group command exercises
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
IBEM 数学公式检测数据集
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
106. How to improve the readability of SAP ui5 application routing URL
MapReduce implements matrix multiplication - implementation code
SVN添加文件时的错误处理:…\conf\svnserve.conf:12: Option expected