Golang Simple Web Service

Source: Internet
Author: User

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

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.