你如何使用 Go 1.16 在子文件夹/包中嵌入功能?

Pre*_*ete 8 go go-http

Go 1.16 已经发布,我想使用新的嵌入功能。如果一切都在主包中,我可以让它工作。但是不清楚如何处理从子文件夹/包访问资源。尝试通过 embed.FS 支持来实现。

例如,我在处理程序包/文件夹中有一个 main.go 和一个 HTTP 处理程序

如果我将处理程序放在主程序中,它就可以工作。如果我把它放在处理程序包中,它找不到模板。我得到:

handlers/indexHandler.go:11:12: pattern templates: no matching files found exit status 1

同样,如果我从 / 提供它,我可以让它从静态文件夹提供图像。但是我不能同时提供来自 / 的处理程序和来自 / 的静态/图像。如果我将图像放在 /static/ 上,它找不到图像。

我认为这与相对路径有关。但是我无法通过反复试验找到正确的组合......依赖这些功能是否为时过早?

以前我用的是 go-rice,我没有这些问题。但我想尽可能多地使用 std 库。

main.go:

package main

import (...)

//go:embed static
var static embed.FS

func main() {

    fsRoot, _ := fs.Sub(static, "static")
    fsStatic := http.FileServer(http.FS(fsRoot))
    http.Handle("/", fsStatic)
    http.HandleFunc("/index", Index)
    http.ListenAndServe(":8080", nil)
}
Run Code Online (Sandbox Code Playgroud)

处理程序/indexHandler.go:

package handlers

import (...)

//go:embed templates
var templates embed.FS

// Index handler
func Index(w http.ResponseWriter, r *http.Request) {

    tmpl := template.New("")
    var err error
    if tmpl, err = tmpl.ParseFS(templates, "simple.gohtml"); err != nil {
        fmt.Println(err)
    }
    if err = tmpl.ExecuteTemplate(w, "simple", nil); err != nil {
        log.Print(err)
    }    
}
Run Code Online (Sandbox Code Playgroud)

结构如下...

.
??? go.mod
??? go.sum
??? handlers
?   ??? indexHandler.go
??? main.go
??? static
?   ??? css
?   ?   ??? layout.css
?   ??? images
?       ??? logo.png
??? templates
    ??? simple.gohtml
Run Code Online (Sandbox Code Playgroud)

Pre*_*ete 32

我终于弄明白了...

您可以将模板文件夹保留在主文件夹中并从那里嵌入它们。然后您需要将 FS 变量注入到其他处理程序包中。弄清楚之后总是很容易。

例如

package main

//go:embed templates/*
var templateFs embed.FS

func main() {
    handlers.TemplateFs = templateFs
...
Run Code Online (Sandbox Code Playgroud)
package handlers

var TemplateFs embed.FS

func handlerIndex() {
    ...
    tmpl, err = tmpl.ParseFS(TemplateFs, "templates/layout.gohtml",...
...
Run Code Online (Sandbox Code Playgroud)

  • @GoForth是的,我可能没有使用正确的措辞,但我的意思是使用../如果你有cmd/main.go,你就不能做../templates/*但是感谢你的回复。 (14认同)
  • 感谢这个解决方案。无法使用相对路径是很烦人的。 (12认同)