Switch language
zh
Switch theme
Light

Go File Server

使用 go 内置 http 快速开启文件服务器


func main() {
	// 方式1
	// http.ListenAndServe(":8080", http.FileServer(http.Dir("./public")))

	// 方式2: 可自动生成对应文件列表页面
    fs := http.FileServer(http.Dir("public"))
    // http.Handle("/public/", http.StripPrefix("/public/", fs))
    // http.Handle("/", http.StripPrefix("/public/", fs))
    http.Handle("/", fs)
    http.ListenAndServe(":8080", nil)
}
🍀