zoukankan      html  css  js  c++  java
  • 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("	Name	:", s.Name)
    	fmt.Println("	Age	:", s.Age)
    	fmt.Println("	Guake	:", s.Guake)
    	fmt.Println("	Price	:", s.Price)
    	fmt.Printf("	Classes	: ")
    	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("	Name	:", s.One)
    	fmt.Println("	Age	:", 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)
    	}
    }
    

      

  • 相关阅读:
    Eclipse启动错误:A Java Runtime Environment(JRE) or Java Development Kit(JDK) must be available……
    thymeleaf 模板使用 提取公共页面
    According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by de
    Spring 自定义Bean 实例获取
    Spring HttpServletRequest对象的获取
    Your ApplicationContext is unlikely tostart due to a @ComponentScan of the defau
    IP 、127.0.0.1、localhost 三者区别
    阿里云 呼叫中心 开发须知
    阿里云 负载均衡 HTTP转HTTPS
    Spring Boot 获取Bean对象实体
  • 原文地址:https://www.cnblogs.com/nazhizq/p/8297997.html
Copyright © 2011-2022 走看看