Go HTTP Service

Source: Internet
Author: User
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
Related Article

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.