最小HTTP服务器

5年以前  |  阅读数:599 次  |  编程语言:Golang 

如下,使用Go编写一个功能最小的HTTP服务器,只是输入URL路径

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", handler) // each request calls handler
    log.Fatal(http.ListenAndServe("localhost:8000", nil))
}

// handler echoes the Path component of the requested URL.
func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
}

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8