Golang--net/http

Source: Internet
Author: User

Build a Web

In the browser input http://localhost:8080

import (    "net/http"    "fmt"    "log")func sayhelloGolang(w http.ResponseWriter, r *http.Request) {    r.ParseForm()  //解析参数,默认是不会解析的    fmt.Println("path", r.URL.Path)    w.Write([]byte("Hello Golang"))}func main() {    http.HandleFunc("/", sayhelloGolang) //设置访问的路由    err := http.ListenAndServe(":8080", nil) //设置监听的端口    if err != nil {        log.Fatal("ListenAndServe: ", err)    }}

Inherit Servehttp

type  BaseHander struct {    }func (hander *BaseHander)ServeHTTP(resp http.ResponseWriter,req *http.Request){      fmt.Println("url path=>",req.URL.Path)      fmt.Println("url param a =>",req.URL.Query().Get("a"))      resp.Write([]byte("hello world"))}func main() {    http.ListenAndServe(":8080",&BaseHander{});}

Net/http Routing

  Package Muximport ("Net/http") type Muxhandler struct {handlers map[string]http. Handler handlefuncs Map[string]func (resp http. Responsewriter, req *http. Request)}func Newmuxhandler () *muxhandler {return &muxhandler{make (map[string]http. Handler), Make (Map[string]func (resp http. Responsewriter, req *http. Request),}}func (handler *muxhandler) servehttp (resp http. Responsewriter, req *http. Request) {urlpath: = req. Url. Path if HL, ok: = Handler.handlers[urlpath]; OK {hl. Servehttp (RESP, req) return} if fn, OK: = Handler.handlefuncs[urlpath]; OK {fn (resp, req) return} http. NotFound (RESP, req)}func (hander *muxhandler) Handle (pattern string, HL http. Handler) {Hander.handlers[pattern] = Hl}func (Handler *muxhandler) Handlefunc (Pattern string, fn func (resp http. Responsewriter, req *http. Request) {Handler.handlefuncs[pattern] = fn}  
  var (port string) Func main () {flag. Stringvar (&port, "Port", ": 8080", "Port to listen") flag. Parse () Router: =mux. Newmuxhandler () router. Handle ("/hello/golang/", &basehander{}) router. Handlefunc ("/hello/world", func (resp http. Responsewriter, req *http. Request) {resp. Write ([]byte ("Hello World")}) http. Listenandserve (port, Router)}  
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.