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.