Go http 库

http 是Go语言的标准库之一,可以使用它创建http服务,同时它也提供http客户端功能。

简单的实例

package main

import (
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        w.Write([]byte("Hello Golang!"))
    })
    http.ListenAndServe(":8080", nil)
}

文件服务器

func main() {
    os.Mkdir("file", 0777)

    http.Handle("/file/", http.StripPrefix("/file/", http.FileServer(http.Dir("file"))))
    http.ListenAndServe(":8080", nil)
}

注:浏览器缓存可能会显示不一致

自定义http配置

http client

GET

POST

表单提交

复杂请求

Last updated

Was this helpful?