gpt4 book ai didi

html - 无法在 golang 中提供静态 html 文件

转载 作者:IT王子 更新时间:2023-10-29 02:23:22 26 4
gpt4 key购买 nike

我正在尝试学习有关 golang 的在线视频类(class),但在提供静态 html 文件时遇到错误。

package main

import (
"io/ioutil"
"net/http"
)

func main() {
http.Handle("/", new(MyHandler))
http.ListenAndServe(":8000", nil)
}

type MyHandler struct {
http.Handler
}

func (this *MyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
path := "public/" + req.URL.Path
data, err := ioutil.ReadFile(string(path))

if err == nil {
w.Write(data)
} else {
w.WriteHeader(404)
w.Write([]byte("404 - " + http.StatusText(404)))
}
}

我的文件夹结构 looks like this .

最佳答案

根据指向您的文件夹结构的链接,我猜测您需要 path := "../public/"+ req.URL.Path。您的应用程序从 bin 目录运行,因此您不能直接进入同级目录,您需要 .. 走出一个目录,此时 public 实际上是一个选项。

关于html - 无法在 golang 中提供静态 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36200382/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com