A simple Web server _golang the Go language implementation

Source: Internet
Author: User

The web is a service based on the HTTP protocol, and the Go language provides a complete net/http package that can be easily built with a Web service that can be run with HTTP packets. Using this package, you can easily set up and manipulate Web routing, static files, templates, cookies, and so on.

HTTP packet Build Web server

Copy Code code as follows:

Package Main

Import (
"FMT"
"Net/http"
"Strings"
"Log"
)

Func sayhelloname (w http. Responsewriter, R *http. Request) {
    r.parseform ()  //parsing parameters, default is not resolved
    FMT. Println (R.form)  //This information is output to the server side of the print information
    FMT. PRINTLN ("Path", R.url.) Path)
    FMT. PRINTLN ("scheme"), R.url. Scheme)
    FMT. Println (r.form["Url_long"])
    for k, V: = range R.form {
      & nbsp Fmt. Println ("Key:", K)
        FMT. Println ("Val:", Strings.) Join (V, ""))
   }
    fmt. fprintf (W, "Hello astaxie!")//This writes to W is the output to the client
}

Func Main () {
http. Handlefunc ("/", Sayhelloname)//Set Access routing
ERR: = http. Listenandserve (": 9090", nil)//set the listening port
If Err!= nil {
Log. Fatal ("Listenandserve:", err)
}
}

The above code, we build, then execute Web.exe, this time actually already in 9090 Port listening for HTTP link request.

Enter http://localhost:9090 in Browser

You can see the browser page output Hello astaxie!

You can try a different address: http://localhost:9090/?url_long=111&url_long=222

What is the output of the browser and what is the server output?

The information that is exported on the server side is as follows:

Figure 3.8 Server-side printed information after a user accesses the web

We see the above code, to write a Web server is very simple, just call the HTTP packet two functions on it.

If you used to be a PHP programmer, you might ask, does our nginx, Apache server need it? Go just doesn't need that because he listens to the TCP port directly, does what nginx do, and sayhelloname this is actually the logical function we wrote, similar to the control layer (Controller) function in PHP.

If you've ever been a Python programmer, you've heard of tornado, the code doesn't look like him, right, yes, go has the features of a dynamic language like Python, and it's easy to write web apps.

If you used to be a ruby programmer, you'd find that a bit like Ror's/script/server startup.

We see go running a Web service with a few simple lines of code, and there are features that support high concurrency within the Web service, and I'll explain in detail in the next two sections how the Go is implementing web-high concurrency.

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.