Go语言中的HTTP与模板编程
1. Go语言HTTP处理函数
在Go语言中,HTTP处理函数有着重要的作用。可以通过 Handle 或 HandleFunc 函数来注册多个处理函数,其函数签名如下:
func Handle(pattern string, handler Handler)
func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
在 ListenAndServe 函数中,第二个参数可以为 nil ,此时请求会被分发到所有已注册的处理函数。每个处理函数应该有不同的URL模式,更具体的模式会优先于更通用的模式。
例如,下面是一个 printenv.go 程序,用于打印环境变量:
/* Print Env */
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
fileServer := http.FileServer(http.Dir("/tmp/www"))
http.Handle("/", fileServer)
http.HandleFunc("/cgi-bin/printenv", printEnv)
err := http.ListenA
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



