This is a creation in Article, where the information may have evolved or changed.
Go to implement an HTTP service is very simple
1, features: Do not need to rely on containers, different from Java and PHP
2, the memory of the process can be used and no third-party storage is required for the configuration file
Service-Side code:
Package Mainimport "FMT" Import ( "Net/http" "Time") Func Myhandle (res http. Responsewriter, req *http. Request) { fmt. Println ("Myhanle") req. Parseform () for key, V:=range req. form{ FMT. Println ("Key:", Key, "value:", v) } Res. Write ([]byte ("Name:" + req. form["Age"][0]))}func MyTest (res http. Responsewriter, req *http. Request) { fmt. Println ("MyTest") req. Parseform () for key, V:=range req. form{ FMT. Println ("Key:", Key, "value:", v) } Res. Write ([]byte ("Name:" + req. form["Age"][0]))}func main () { fmt. Println ("main start") http. Handlefunc ("/root", Myhandle) http. Handlefunc ("/test", MyTest) http. Listenandserve ("127.0.0.1:9999", nil) for{time . Sleep (1E9) }}
Client code:
Package Mainimport "FMT" Import ( "Net/http" "Io/ioutil") func main () { fmt. Println ("main start") rsp, _: = http. Get ("http://127.0.0.1:9999/root?name=for&age=56") data, _:=ioutil. ReadAll (RSP. Body) p:= string (data) FMT. Println (P)}
320 reads ∙1 likes