1.golang Print Input
Package Mainimport "FMT" Func Main () {FMT. Printf ("Hello world!\n")}
Execute the following command
Go Build Print.go
2. Go Web server Go Language standard library-net/http
The Go Web server is built using the standard library provided by the Go Language net/http
, which provides HTTP clients and service-side implementations via HTTP packets. At the same time using this package can be very simple to the Web routing, static files, templates, cookies and other data set and operation.
HTTP Package Build Web server
Package Mainimport ("FMT" "net/http" "Strings" "Log") Func sayhelloname (w http. Responsewriter, R*http. Request) {r.parseform ()//parsing parameters, which are not resolved by defaultFmt. Println (R.form)//This information is output to the server-side printing informationFmt. Println ("Path", R.url. Path) fmt. Println ("Scheme", R.url. Scheme) fmt. Println (r.form["Url_long"]) forK, V: =range R.form {fmt. Println ("Key:", K) fmt. Println ("Val:", strings. Join (V,"") } fmt. fprintf (W,"Hello wrold!")//This write to W is output to the client.}func Main () {http. Handlefunc ("/", Sayhelloname)//to set access routesERR: = http. Listenandserve (": 9090", nil)//set the listening port ifErr! =Nil {log. Fatal ("Listenandserve:", Err)}}
After we compile the above code and run successfully in the IDE, we can listen for HTTP link requests on port 9090 at this time.
We entered the http://ip:9090 in the browser, you can see the browser page to enter the Hello world!
This time if we add some parameters after the browser address try: http://ip:9090?url_long=111&url_long=222,
See what's in the browser output? What is the server-side output?
map[url_long:[111222/scheme [111222111222
We see the above code, to write a Web server is not very simple, just call the HTTP packet of two functions can be.
We see that go has a Web service running with a few simple lines of code, and this Web service has features that support high concurrency.
Golang Simple Web Service