我使用golang來設置服務器,并在其中執行模板。在我的模板內部,我試圖獲得圖像,但由于某種原因,我嘗試的任何東西都不起作用。在控制臺中,它說給出該錯誤:GET http://localhost:5051/static/photo_2021-06-17_14-18-09.jpg 404 (Not Found)圍棋代碼 :package mainimport ( "html/template" "net/http" // "io/ioutil")func main() { http.HandleFunc("/a", indexHandler) http.ListenAndServe(":5051", nil) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))}func indexHandler(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles("templates/main.html") t.ExecuteTemplate(w, "main", struct{}{})}模板代碼:{{ define "main" }}<html> <head> <meta charset="UTF-8"> </head> <body> <div> <img src="../static/photo_2021-06-17_14-18-09.jpg"> </div> </body></html>{{ end }}謝謝!
嘗試在 golang 模板中顯示圖像
慕蓋茨4494581
2022-09-05 17:58:08