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