import ( "net/http")func main() { http.Handle("/", http.FileServer(http.Dir("static"))) http.ListenAndServe(":8080", nil)}我導(dǎo)航到 localhost:8080/ 并收到 404 錯(cuò)誤。我究竟做錯(cuò)了什么?
2 回答

皈依舞
TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊
具有以下結(jié)構(gòu):
main.go
static
|- index.html
并main.go包含:
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("static")))
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
使用 運(yùn)行您的解決方案后go run main.go,您應(yīng)該能夠轉(zhuǎn)到localhost:8080/并最終獲得index.html.
如果它不起作用,也許添加的錯(cuò)誤處理可能會(huì)有所幫助。代碼是正確的。
- 2 回答
- 0 關(guān)注
- 242 瀏覽
添加回答
舉報(bào)
0/150
提交
取消