Go Learning (iv) Simple website

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. First, write before:




These days upon hearing go out of version 1.1. The net performance is twice times the original. Of course there are a lot of opposing voices. But as a person who is bent on going, he is thinking. How did I upgrade? How did it get upgraded?




Win Needless to say, re-download directly.


Linux:


$ CD GO/SRC
$ HG Pull
$ HG Update release
$./all.bash






Ha, it's so simple!




Second, start the web


The code is simple. I never thought it would be so easy! :




Package Main


Import (
"IO"
"Net/http"
"Log"
)


Hello world, the Web server
Func HelloServer (w http. Responsewriter, req *http. Request) {
Io. WriteString (W, "Hello, world!\n")
}


Func Handler (w http. Responsewriter, req *http. Request) {
Io. WriteString (W, "It works!\n")
}


Func apilist (w http. Responsewriter, req *http. Request) {

}


Func Main () {
http. Handlefunc ("/", Handler)
http. Handlefunc ("/hello", HelloServer)
http. Handlefunc ("/list", Apilist)
ERR: = http. Listenandserve (": 12345", nil)
If err! = Nil {
Log. Fatal ("Listenandserve:", err)
}
}






Explain the HA page needless to say. Just describe the package in which it is located.
Import (
"IO"
"Net/http"
"Log"
)
Is the focus, go provides a lot of built-in packages to help us work, they call convenient hehe.
A simple Web server. Just use the HTTP package.


The following main is already very clear, do a web of all know URL rewrite. The contents of the main function are equivalent to this. It's just that he rewrites all of your overrides to function processing.
/give Handel treatment/hello to HelloServer processing/list to Apilist


Then let it monitor Port 12345.




Compile and then enter http://localhost:12345 in your browser and you will see the contents of the Handel function output.
This is go!.












This period of time is busy, with the new slow.
Please look forward to it next time. Make a more complicated website huh?
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.