添加 go.mod 文件后,我無法從 App Engine 上的 golang 訪問 HTML 模板文件。一切都在本地進行。我已使用 Stackdriver Debug 驗證 App Engine 上是否存在 HTML 文件,但運行時看不到它們。這是我的代碼。var templates map[string]*htmltpl.Templatefunc init() { if templates == nil { templates = make(map[string]*htmltpl.Template) } templatesDir := getTemplatesDir() layouts, err := filepath.Glob(templatesDir + "/layouts/*.html") if err != nil { panic(err) } includes, err := filepath.Glob(templatesDir + "/includes/*.html") if err != nil { panic(err) } // Generate our templates map from our layouts/ and includes/ directories for _, layout := range layouts { files := append(includes, layout) templates[filepath.Base(layout)] = htmltpl.Must(htmltpl.ParseFiles(files...)) }}func getTemplatesDir() string { _, filename, _, ok := runtime.Caller(1) if !ok { panic("Could not get filename") } dirpath := path.Join(path.Dir(filename), "../../templates") return dirpath}這是我的app.yaml:runtime: go111main: ./mainhandlers: - url: .* script: auto secure: always這是我的目錄結(jié)構(gòu):.├── app.yaml├── db/├── go.mod├── go.sum├── handlers/├── main│ └── main.go├── middleware/├── models/├── static/├── templates/│ ├── includes│ │ ├── base.html│ │ ├── button.html│ │ ├── message.html│ │ └── profile.html│ └── layouts│ └── thread.html└── utils └── template └── template.go我不明白為什么在 App Engine 上,調(diào)用filepath.Glob(templatesDir + "/layouts/*.html")返回一個空切片,而它返回一個包含本地運行時的路徑的切片thread.html。
1 回答

鴻蒙傳說
TA貢獻1865條經(jīng)驗 獲得超7個贊
該函數(shù)runtime.Caller()
返回編譯時源文件路徑。該應(yīng)用程序未在與其編譯的目錄相同的目錄中運行。
應(yīng)用程序運行時當前工作目錄設(shè)置為包含 app.yaml 的目錄。使用此函數(shù)獲取模板目錄:
func getTemplatesDir() string { return "templates" }
- 1 回答
- 0 關(guān)注
- 156 瀏覽
添加回答
舉報
0/150
提交
取消