This is a creation in Article, where the information may have evolved or changed. Do a simple sample through the HTTP protocol server and client interaction. Bar inside a lot of posts, but not a complete sample, here to do a groping summary, hope for the later learning go language of the small partners to help. Server-side code: "' Gopackage mainimport (" FMT "" Net/http ") func main () {FMT. PRINTLN ("Server listening:") http. Handlefunc ("/con", Conncet) http. Listenandserve (": 8080", nil)} func conncet (w http. Responsewriter, R *http. Request) {r.parseform () Data: = R.url. Query () fmt. PRINTLN ("All data", data) var nameexist, idexist bool var id,name []string if ID, nameexist = r.form["id"]; nameexist{FMT. Println ("Id is", id[0])} if Name, idexist = r.form["name"]; idexist{FMT. Println ("Name is", name[0])} if Nameexist && idexist{w.write ([]byte (FMT. Sprintf ("Welcome%s to come in", Name[0]))}} "description: Server waits for a client connection by listening on the local 8080 port. Start the server first and print the following: "C:/go\bin\go.exe run e:/go_code/gopath/src/example/http/httpserver.goserver listening:all data MAP[ID:[321] Name:[agent]]id is 321Name was Agentall data map[name:[agent] Id:[321]]id is 321Name is Agent ' client code, Clie NT reads the configuration file to get the IP address of the service and the address of the directory to which it is accessed. "' GopaCkage mainimport ("FMT" "Net/http" "Io/ioutil" "Encoding/json"//"Net/url") type serverinfo struct {Ip stringport Stringdir string}type clientinfo struct {Name stringid string}type configdata struct {serverdata serverinfo ClientData Cl Ientinfo}func Main () {FMT. Println ("Client con:") Client: = &http. Client{}serveinst, Clientinst, err: = conInst.ServerData.GetServerInfoFromConf () if err! = nil{fmt. PRINTLN (err) Return}url: = Serveinst.constructurl () Urlpara: = Clientinst.constructurl (&url) req, e: = http. Newrequest ("Get", Urlpara, nil) if E!=nil{fmt. Println (e)}//v: = req. Form//v.set ("Name", Clientinst.name)//v.set ("Id", clientinst.id) RSP, ERR1: = client. Do (req) if err1! = nil{fmt. PRINTLN ("Client Get err.", ERR1) return}defer RSP. Body.close () data, err2: = Ioutil. ReadAll (RSP. Body) If err2 = = Nil {fmt. Println (String (data))}fmt. PRINTLN ("Client Over", ERR2)}var coninst = Newconfinfo () func newconfinfo () *configdata{return &configdata{}}func (s *serverinfo) getserverinfofromconf () (*serverinfo, *clieNtinfo,error) {conf: = &configdata{}data, err: = Ioutil. ReadFile ("Conf/info.json") if err! = nil{fmt. Println ("Readdir failed, err is", err) return NIL,NIL,ERR}ERR1: = json. Unmarshal (data,conf) if err1! = nil {fmt. Println ("Unmarshal failed, err is", ERR1) return nil, nil,err1}fmt. Println ("Get info is", conf) return &conf. Serverdata, &conf. Clientdata,nil}func (S *serverinfo) Constructurl () string {URL: = "/http" + S.ip + ":" + S.port + "/" + S.DIRFMT.PRINTLN ( "URL is", url) return Url}func (S *clientinfo) constructurl (URL *string) string {//*url =*url + "?" + "Name" + "=" + s.name*u RL =*url + "?" + "Name" + "=" + S.name + "&" + "Id" + "=" + s.idfmt.println ("URL is", *url) return *url} ' clinet print as follows: ' "C:/go\bin\go.exe Run E:/go_code/gopath/src/example/http/httpclient.goclient con:get info is &{{127.0.0.1 8080 Con} {Agent 321}}url is Http://127.0.0.1:8080/conurl is Http://127.0.0.1:8080/con? Name=agent&id=321welcome agent to come inclient through <nil>process finished withExit code 0 "' JSON file is as follows: ' ' ' json{' serverdata ': {" Ip ":" 127.0.0.1 "," Port ":" 8080 "," Dir ":" Con "}," Clientdata ": {" Name ": "Agent", "ID": "321"}} "186 reads
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