zoukankan      html  css  js  c++  java
  • 9.5 处理http 请求

    package main
    
    import (
    	"fmt"
    	"net/http"
    )
    
    func main() {
    
    	mux := http.NewServeMux()
    	mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
    		if r.Method == http.MethodGet {
    			fmt.Fprintln(w, "User GET")
    		}
    		if r.Method == http.MethodPost {
    			fmt.Fprintln(w, "User POST")
    		}
    	})
    
    	// separate handler
    	itemMux := http.NewServeMux()
    	itemMux.HandleFunc("/items/clothes", func(w http.ResponseWriter, r *http.Request) {
    		fmt.Fprintln(w, "Clothes")
    	})
    	mux.Handle("/items/", itemMux)
    
    	// Admin handlers
    	adminMux := http.NewServeMux()
    	adminMux.HandleFunc("/ports", func(w http.ResponseWriter, r *http.Request) {
    		fmt.Fprintln(w, "Ports")
    	})
    
    	mux.Handle("/admin/",
    		http.StripPrefix("/admin", adminMux))
    
    	// Default server
    	http.ListenAndServe(":8080", mux)
    
    }
    
    /*
    (sx3.5.3) ➜  ~ curl -XI http://127.0.0.1:8080/user
    (sx3.5.3) ➜  ~ curl -XI http://127.0.0.1:8080/userA
    404 page not found
    (sx3.5.3) ➜  ~ curl -X POST http://127.0.0.1:8080/user
    User POST
    
    (sx3.5.3) ➜  ~ curl  http://127.0.0.1:8080/admin
    <a href="/admin/">Moved Permanently</a>.
    */
    
    
  • 相关阅读:
    S3C2440实现dm9000网卡驱动程序移植
    IMX257虚拟网卡vnet驱动程序
    ram_flash驱动
    S3C2440 nor_flash驱动程序
    Java 打印* 三角形
    Java系列学习说明
    java案例1,打印hello java
    zabbixproxy安装
    python鉴黄程序
    mssql发布订阅事项
  • 原文地址:https://www.cnblogs.com/zrdpy/p/8654883.html
Copyright © 2011-2022 走看看