Gcloud函数部署找不到Golang模板文件

问题描述 投票:0回答:2

我已经编写了一些Golang代码,这些代码在我的本地计算机上进行测试时就可以使用。当我将其部署为Google Cloud函数时,它失败了,因为它无法打开模板文件。代码失败的行是:

    t, err := template.New("list.gohtml").ParseFiles("list.gohtml")

此呼叫之后,err设置为open list.gohtml: no such file or directory

该文件与go源文件位于同一目录中,并且未在.gcloudignore.gitignore中列出。 gcloud函数文档说,除非在其中一个忽略文件中列出,否则目录中的所有文件都将上载;如果我运行gcloud meta list-files-for-upload,则文件list.gohtml is会显示在显示的列表中。

是否有一些不可思议的文件夹布局可以完成这项工作,或者gcloud functions deploy命令的选项?

go google-cloud-functions gcloud go-templates
2个回答
1
投票

我创建了一个枚举上传文件的函数:

func L(w http.ResponseWriter, r *http.Request) {
    var files []string

    err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
        files = append(files, path)
        return nil
    })
    if err != nil {
        panic(err)
    }
    for _, file := range files {
        fmt.Fprintln(w, file)
    }
}

输出:

.
go.mod
go.sum
main.go
serverless_function_source_code
serverless_function_source_code/f.go
serverless_function_source_code/go.mod
serverless_function_source_code/test.tmpl

重写模板使用功能:

tmpl, err := template.New("test.tmpl").ParseFiles("serverless_function_source_code/test.tmpl")

Works!

但是,这是未记录的,并且是黑客:-(

另一种方法是将模板作为字符串嵌入Golang文件中。

我建议通过Google的Cloud Functions的问题跟踪器提交功能请求,>


0
投票

基于@DazWilkin的回复,我现在在服务函数的开头调用以下函数。

© www.soinside.com 2019 - 2024. All rights reserved.