Go HTTP Server Create

Introduction

HTTP servers are even easier to set up and use:

package main /*w  ww.  ja va  2  s. c  o m*/

import ("net/http" ; "io") 

func hello(res http.ResponseWriter, req *http.Request) { 
    res.Header().Set( 
        "Content-Type", 
        "text/html", 
    ) 
    io.WriteString( 
        res, 
        `<DOCTYPE html> 
        <html> 
          <head> 
              <title>Hello, World</title> 
          </head> 
          <body> 
              Hello, World! 
          </body> 
        </html>`, 
    ) 
} 
func main() { 
    http.HandleFunc("/hello", hello) 
    http.ListenAndServe(":9000", nil) 
} 



PreviousNext

Related