003_go Start Httpserver

Source: Internet
Author: User

One

The HTTP service is developed in Golang and can be provided using the Net/http package. The Net/http package provides a very full functionality, and the HTTP service that is launched is also very stable and efficient , and can be used in production environments entirely.

Package Mainimport (    "FMT"    "Log" "    net/http")//W represents the Response object, the content returned to the client is processed in the object//R represents the client request object, which contains the request header, Request parameters, etc. func index (w http. Responsewriter, R *http. Request) {    //writes content to W, and the FMT is output in the browser    . fprintf (W, "Hello Golang http!")} Func Main () {    //sets the route, if access/, calls the Index method    http. Handlefunc ("/", index)    //Start Web service, listen on 9090 port    Err: = http. Listenandserve (": 8000", nil)    if err! = Nil {        log. Fatal ("Listenandserve:", Err)    }}

To run the service:

Go Run main.go

You can see the Hello Golang http! output by accessing http://localhost:8000 in the browser.

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.