Golang HTTP client request for the Getting Started case

Source: Internet
Author: User

Send requests, receive transfers, and resolve

Package Mainimport ("FMT" "Net/http" "Io/ioutil" "Net/url" "Encoding/json" "OS") type Student struct {Name stringage INTG Uake boolclasses []stringprice 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 ', ' both string ' JSON: ' One ' '}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 () {//Parse fixed structure jsonst: = &student{"Xiao Ming", 16,true,[]string{"Math", "中文版", "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"]} ')////parse unknown structure json//var f interface{}//err = JSON . Unmarshal (b, &f)//checkerror (ERR)//This is a key value pair that is stored in 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 what to handle")//}//}}func CheckError (err error) {if err! = Nil {fmt. fprintf (OS. Stderr, "Fatal Error:%s", err. Error ()) OS. Exit (1)}}

  

Golang HTTP client request for the Getting Started case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.