一,get方式的php代码和返回:
代码:
res.php
<?php
???$data = [
???'code'=>0,
???'msg'=>'success',
???'data'=>$_GET
???];
???echo json_encode($data);
???exit;
?>
返回:
说明:刘宏缔的go森林是一个专注golang的博客, ? ? ? ? ??地址:https://blog.csdn.net/weixin_43881017
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,go代码:
testController.go
?//用户信息info
func (u *TestController) Get(c *gin.Context) {
????//得到url,发起请求
????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()
????//得到返回结果
????body, _ := ioutil.ReadAll(res.Body)
????bodystr := string(body)
????//对返回的json数据做解析
????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解析出错:");
????????fmt.Println(err.Error());
????}
????//返回
????m := map[string]interface{} {
????????"resCode":code,
????????"username":username,
????}
????resp := result.NewResult(c)
????resp.Success(m)
}
?
返回:
三,查看go的版本:
root@lhdpc:~# go version
go version go1.16.4 linux/amd64
|