The go language implements a simple static Web server _golang

Source: Internet
Author: User

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

In the event of a war, Java is generally the role of aircraft carrier battle groups.
Once deployed, is frigates, cruisers, aircraft carrier aircraft, early warning aircraft, electronic warfare aircraft, submarines and so on
Mighty, Kill will pass.
(JVM, dozens of jar packs, Tomcat middleware, SSH framework, various configuration files ...) Born to be heavyweight, dedicated to large-scale warfare.

And the go language is more like a F35 fighter bomber.
Single-handedly, silently, drop the bomb and leave.
Exclusive bombers, the air combat will also be a little bit.
If you can't make it, call it brother F22.
(Go is a compiled language that requires no reliance, no virtual machines, calls to C code and is simple enough, but very comprehensive)

Plan the knowledge points for Go language learning

1. Build HTTP Service
2. Connecting to the database
3. Local IO
4. Multithreading
5. The Network
6. Invoke Local command
7. Invoke C language code

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

Httpserver.go

Copy Code code as follows:

Package Main

Import (
"Flag"
"Io/ioutil"
"Log"
"Net/http"
"OS"
"Strings"
)

var Realpath *string

Func 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 Internet:

Copy Code code as follows:

Package Main

Import (
"Net/http"
)

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

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

Execute under the Gopath

Copy Code code as follows:

Go run Httpserver.go--path=/tmp/static

View Web page, everything is OK.

So the go language takes less than 50 lines of code, and after compiling less than 7M of executable files, a simple static server is implemented.

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.