zoukankan      html  css  js  c++  java
  • GoWeb编程之多路复用

    GoWeb编程多路复用

    在web编程中,比如我们要一个Url对应一个处理函数,或者一个页面对应一个静态文件,这些都需要进行处理,这个时候就是我们多路复用派上用场了。

    package main
    
    import "net/http"
    import "fmt"
    
    func index(writer http.ResponseWriter, request* http.Request) {
    	fmt.Fprintf(writer, "index" )
    }
    
    
    func main() {
    
    	//创建一个Http多路复用器
    	mux := http.NewServeMux()
    
    	//定义一个Http文件服务器,本机的绝对路径(大家可以试试自己机器上)
    	files := http.FileServer(http.Dir("/Users/xxx/Desktop/GoApplication/static"))
    	
    	//去掉URL路径前缀,返回指定文件
    	mux.Handle("/static/", http.StripPrefix("/static", files))
    	
    	//接收到URL为 ‘/’ 交给 index 函数处理
    	mux.HandleFunc("/", index)
    	
    	//指定端口,传递多路复用器
    	server := &http.Server{
    		Addr : ":8080",
    		Handler : mux,
    	}
    	server.ListenAndServe()
    	
    }
    
    
    
    
    
    
    
    
  • 相关阅读:
    bootstrap
    bootstrap
    bootstrap
    【k8s】Pod-terminationGracePeriodSeconds
    【k8s】Pod-tolerations
    【k8s】Pod-nodeSelector
    【k8s】Pod-nodeName
    【k8s】Pod-hostname
    【k8s】Pod-hostPID
    【k8s】Pod-hostNetwork
  • 原文地址:https://www.cnblogs.com/chenchengxun/p/8609403.html
Copyright © 2011-2022 走看看