gin的HTMLGlob不适用**。

huangapple go评论65阅读模式
英文:

gin HTMLGlob not applying **

问题

我有一个使用模板的 gin v1.8.1 Go 程序。主要的 Go 文件是 cmd/app/app.go。在其中,我有以下代码:

	r := gin.Default()

	cwd, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	fmt.Printf("CWD is %s", cwd)
	r.LoadHTMLGlob("../../templates/**/*")

当我运行时,会出现以下 panic 错误:

CWD is /Users/paul/src/personal/app/cmd/app
panic: html/template: pattern matches no files: `../../templates/**/*`

goroutine 1 [running]:
html/template.Must(0x0, {0x4aac5a0, 0xc000584490})
	/usr/local/Cellar/go/1.18.3/libexec/src/html/template/template.go:374 +0x8b
github.com/gin-gonic/gin.(*Engine).LoadHTMLGlob(0xc0005b04e0, {0x4a2e2ae, 0x14})
	/Users/paul/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/gin.go:251 +0x15e
main.main()
	/Users/paul/src/personal/app/cmd/app/app.go:31 +0x169

当我移除双星号 (r.LoadHTMLGlob("../../templates/*")) 后,它可以正常工作。我知道标准库的 globber 不支持双星号,但是 gin 文档 中说他们的 globber 支持双星号。

英文:

I have a gin v1.8.1 go program that uses templates. Main go file is cmd/app/app.go. Inside it, I have this

	r := gin.Default()

	cwd, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	fmt.Printf("CWD is %s", cwd)
	r.LoadHTMLGlob("../../templates/**/*")

When I run I get this panic

CWD is /Users/paul/src/personal/app/cmd/app
panic: html/template: pattern matches no files: `../../templates/**/*`

goroutine 1 [running]:
html/template.Must(0x0, {0x4aac5a0, 0xc000584490})
	/usr/local/Cellar/go/1.18.3/libexec/src/html/template/template.go:374 +0x8b
github.com/gin-gonic/gin.(*Engine).LoadHTMLGlob(0xc0005b04e0, {0x4a2e2ae, 0x14})
	/Users/paul/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/gin.go:251 +0x15e
main.main()
	/Users/paul/src/personal/app/cmd/app/app.go:31 +0x169

When I remove the double star (r.LoadHTMLGlob("../../templates/*")), it works fine. I know the standard library globber does not handle double star, but gin docs says theirs does

答案1

得分: 0

显然,我刚刚误解了这个globbing实现的工作原理。我添加了一个新的模板test/index.html,并尝试使用语法../../templates/**/*。在调试模式下运行的Gin会列出它找到的所有模板。但它只找到了index.html,而没有列出基本目录中的其他模板。**的意思是匹配一个必须存在的目录。

英文:

Apparently I have just misunderstood how this implementation of globbing works. I added a new template, test/index.html and tried the syntax ../../templates/**/*. Gin running in debug mode lists all of the templates it finds. Rather than listing the templates in the base directory, it only finds index.html. The ** means match a directory that must exist.

huangapple
  • 本文由 发表于 2022年6月24日 20:07:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/72743846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定