My first go web app to commemorate a moment

Source: Internet
Author: User

Refer to go Web programming, very simple program:

The approximate steps:

      1. Binding IP and Ports
      2. Bind the corresponding processor or processor function, there are two options, select one to listen to IP and port
        1. Processor:
          1. Define a struct structure body
          2. Then let the struct implement the Servehttp interface.
          3. Create an instance of the struct
          4. Pass the instance's address (pointer) as a parameter to the handle
        2. Processor functions
          1. Define a function
          2. The function must be signed with the same function as Servehttp
          3. Function names are passed directly as parameters to Handlefunc
      3. Access the bound IP plus port
using the processor form:
Package Mainimport ("FMT" "net/http") type Home struct{}type about Struct{}func (H *home) servehttp (w http. Responsewriter, R *http. Request) {fmt. fprintf (W, "This Is Home")}func (a *about) servehttp (w http. Responsewriter, R *http. Request) {fmt. fprintf (W, "This was about")}func main () {server: = http. SERVER{ADDR: "127.0.0.1:8080",}home: = &home{}about: = &about{}http. Handle ("Home") http. Handle ("/about", about) server. Listenandserve ()}

  

using the Processor function form:
Package Mainimport ("FMT" "Net/http") Func Home (w http. Responsewriter, R *http. Request) {fmt. fprintf (W, "This was Home2")}func about (w http. Responsewriter, R *http. Request) {fmt. fprintf (W, "This is About2")}func Main () {server: = http. SERVER{ADDR: "127.0.0.1:8080",}http. Handlefunc ("Home") http. Handlefunc ("/about", about) server. Listenandserve ()}

  

My first go web app to commemorate a moment

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.