在 Google App Engine 中托管 Go 項(xiàng)目時(shí)應(yīng)如何處理文件路徑?
我正在嘗試在 Google 的應(yīng)用程序引擎上部署簡(jiǎn)單的 go 語(yǔ)言代碼。這是我試圖部署的代碼。 https://github.com/GoogleCloudPlatform/golang-samples/tree/master/appengine/go11x/static主程序package mainimport ( "fmt" "html/template" "log" "net/http" "os" "path/filepath" "time")var ( indexTmpl = template.Must( template.ParseFiles(filepath.Join("templates", "index.html")), ))func main() { http.HandleFunc("/", indexHandler) // Serve static files out of the public directory. // By configuring a static handler in app.yaml, App Engine serves all the // static content itself. As a result, the following two lines are in // effect for development only. public := http.StripPrefix("/public", http.FileServer(http.Dir("public"))) http.Handle("/public/", public) port := os.Getenv("PORT") if port == "" { port = "8080" log.Printf("Defaulting to port %s", port) } log.Printf("Listening on port %s", port) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))}// indexHandler uses a template to create an index.html.func indexHandler(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } type indexData struct { Logo string Style string RequestTime string } data := indexData{ Logo: "/public/gcp-gopher.svg", Style: "/public/style.css", RequestTime: time.Now().Format(time.RFC822), } if err := indexTmpl.Execute(w, data); err != nil { log.Printf("Error executing template: %v", err) http.Error(w, "Internal server error", http.StatusInternalServerError) }}問(wèn)題:如何處理我希望應(yīng)用程序讀取的模板和其他小文件?我的應(yīng)用程序是一個(gè)玩具應(yīng)用程序,因此我不需要云存儲(chǔ)或任何此類(lèi)解決方案。我只想從(本地)目錄中讀取內(nèi)容。
查看完整描述