The go language implements a simple static Web server

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The Android SDK document is very slow to open and does not support searching. Therefore, it is necessary to build a static server locally. The following is a reference to the resources of other blogs, thanks to the author.

First, build a static server I write programs like to use HTML to send JSON requests to backend processing via Ajax. The Httpserver.go code is as follows:

package mainimport  (         "Flag"           "Io/ioutil"          "Log"           "Net/http"          "OS"           "Strings") Var realpath *stringfunc staticresource (w http. Responsewriter, r *http. Request)  {        path := r.url. Path        request_type := path[strings. LastIndex (path,  "."):]        switch request_type {         case  ". css":                 w.header (). Set ("Content-type",  "Text/css")         case  ". js":                 w.header (). Set ("Content-type",  "Text/javascript")         default:         }         fin, err  := os. Open (*realpath + path)         defer fin. Close ()         if err != nil {                 log. Fatal ("Static resource:",  err)         }          fd, _ := ioutil. ReadAll (Fin)         w.write (FD)}func main ()  {         realpath = flag. String ("Path", " ", " Static resource path ")         flag. Parse ()         http. Handlefunc ("/",  staticresource)         err := http. Listenandserve (": 8080",  nil)         if err != nil  {                log. Fatal ("Listenandserve:",  err)         } }

The more BT method:

Package Mainimport ("Net/http") func Main () {http. Handle ("/", HTTP. Fileserver (http. Dir ("/tmp/static/"))) HTTP. Listenandserve (": 8080", nil)}

Extract the Easyui front-end framework into the/tmp/static directory:

Executed under the Gopath

Go run Httpserver.go--path=/tmp/static
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.