This is a creation in Article, where the information may have evolved or changed.
2014-02-05 WCDJ
0 Summary
Previously summed up how to use Perl to build Web Services "Web server implementation (the smallest Perl Web server)." This article describes how to use Golang to build a Web service, and the Go language provides a complete net/http package, which makes it easy to build a Web service that can run, while using this package to easily route web, static files, templates, Cookies and other data to be set up and operated.
1 Test Code One
Http_svr.go
Package Mainimport ("FMT" "Log" "Net/http" "strings") Func Sayhelloname (w http. Responsewriter, R *http. Request) {//parse parameter, default is r not resolved. Parseform ()//This information is output to the server side of the print information FMT. PRINTLN ("Request map:", R.form) fmt. PRINTLN ("Path", R.url. Path) fmt. PRINTLN ("scheme", R.url. Scheme) fmt. Println (r.form["Url_long"]) for k, V: = Range R.form {fmt. Println ("Key:", K) fmt. Println ("Val:", Strings. Join (V, ";"))} This write to W information is output to the client's FMT. fprintf (W, "Hello gerryyang!\n")}func Main () {//sets the route HTTP for access. Handlefunc ("/", Sayhelloname)//Set the listening port err: = http. Listenandserve (": 9090", nil) if err! = Nil {log. Fatal ("Listenandserve:", Err)}}
2 Test Case One
(1) In the browser input: http://localhost:9090/, then:
Browser output:
Hello gerryyang!
HTTP_SVR output:
Request map:map[]
Path/
Scheme
[]
(2) Use the Curl command in the terminal to test.
Curl "Http://localhost:9090/?url_long=111&url_long=222&name=gerryyang&gender=male"
Terminal output:
Hello gerryyang!
HTTP_SVR output:
Request map:map[url_long:[111 222] Name:[gerryyang] Gender:[male]
Path/
Scheme
[111 222]
Key:url_long
val:111;222
Key:name
Val:gerryyang
Key:gender
Val:male
3 Test Code Two
Package Mainimport ("FMT" "net/http") type Mymux struct {}func (P *mymux) servehttp (w http. Responsewriter, R *http. Request) {if r.url. Path = = "/" {Sayhelloname (W, R) return}http. NotFound (W, R) Return}func Sayhelloname (w http. Responsewriter, R *http. Request) {fmt. fprintf (W, "Hello Gerryyang, Version 2!\n")}func Main () {mux: = &mymux{}http. Listenandserve (": 9090", MUX)}
4 test Case Two
Gerryyang@mba:go_web$curl "http://localhost:9090/?url_long=111&url_long=222&name=gerryyang&gender= Male "Hello Gerryyang, version 2!gerryyang@mba:go_web$curl" http://localhost:9090/aa?url_long=111&url_long=222 &name=gerryyang&gender=male "404 Page Not Found
5 Summary
Use case one, write a Web service using Golang as long as you call the HTTP packet of two functions, do not rely on the Nginx,apache server.
6 Principle Analysis
Golang the process of implementing Web Services is as follows:
(1) Create listen Socket, listen to the specified port, wait for the client (large duck) request to arrive;
(2) The Listen socket accepts the client's request, obtains the customer socket, and then communicates with the client through the clients socket;
(3) Processing the client's request, first read the HTTP request protocol header from the client socket, if it is the post method, you may also want to read the data submitted by the client, and then hand over to the corresponding handler processing request, handler processing finished ready for the client to prepare the data, addressed to clients via client sockets;
7 References
[1] Go Web programming