golang 返回html文件
可以使用Go语言的net/http包来返回HTML文件。以下是一个示例代码:
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
http.ListenAndServe(":8080", nil)
}
在这个示例中,我们使用http.HandleFunc()来设置处理请求的函数。当请求根路径时,我们使用http.ServeFile()方法将文件index.html返回给客户端。最后,我们使用http.ListenAndServe()方法启动HTTP服务器并监听端口8080。
确保在同一目录下有index.html文件。
原文地址: http://www.cveoy.top/t/topic/eaZT 著作权归作者所有。请勿转载和采集!