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.

Some of the feelings of learning go language are not necessarily accurate.

If there is a war, Java is generally the role of the carrier battle group.
Once dispatched, it is frigate, cruiser, carrier aircraft, early warning machine, electronic warfare aircraft, submarine, etc.
Mighty, Kill will pass.
(JVM, dozens of jar packages, Tomcat middleware, SSH framework, various configuration files ...) Born to be heavyweight, specifically for large-scale warfare)

And the go language is more like a F35 fighter bomber.
Single-handedly, silently, dropping bombs and leaving.
Exclusive bomber, air combat will be a little bit.
Really can not make, call it eldest brother F22.
(Go is a compiled language that doesn't need to be relied on, doesn't need a virtual machine, can call C code and it's simple enough, but very comprehensive)

Planning the knowledge points for Go language learning

1. Setting up an HTTP service
2. Connect to the database
3. Local IO
4. Multithreading
5. Network
6. Calling local commands
7. Calling the C language code

First, build a static server.
I write programs like using HTML to send JSON requests to back-end processing via Ajax.

Httpserver.go

Package Mainimport ("Flag" "Io/ioutil" "Log" "Net/http" "OS" "strings") var real Path *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)}}

See a more BT method on the Web:

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

Copy CodeThe code is as follows:
Go run Httpserver.go--path=/tmp/static

View Web page, everything is OK.

So the go language in less than 50 lines of code, compiled less than 7M of executable files, the implementation of a simple static server.

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.