


How to parse an embed.FS template using the template.ParseFS function
php editor Xiaoxin brings you a guide on how to use the template.ParseFS function to parse the embed.FS template. When developing projects using the Go language, we often use the embed package to embed static files, and the template.ParseFS function can help us parse these embedded template files. This article will introduce in detail how to use the template.ParseFS function to help you process template files more flexibly during the development process and improve the development efficiency of the project. Let’s take a look!
Question content
I want to parse all templates in the same template.Template
structure but I don't know how to parse and it also gives me an error. I have next code:
package main import ( "embed" "html/template" "log" "os" ) //go:embed internal/web/views/* var viewsFS embed.FS func main() { tmpls, err := template.New("").ParseFS(viewsFS, "**/*.html") if err != nil { log.Fatal(err) // Debugging I finded out that the error is here so the code below is irrelevant } tmpls.ExecuteTemplate(os.Stdout, "pages/home", nil) }
Give me ParseFS
The error in the method is next:
$ 2023/09/16 23:36:42 template: pattern matches no files: `**/*.html`
I think the error is in the patterns
parameter of the ParseFS
method, I don't know.
I have many html files in the internal/web/views
directory. In fact, all the files in this folder are html files, and each file has one or more { {define}}
type template. p>
Any help would be greatly appreciated, thank you
Solution
@Charlie-Tumahai Credits this Documentation (Go Package Official Website) Global patterns in Go
So, in order to parse all templates in the same template.Template
structure, I have to do the following:
package main import ( "embed" "html/template" "log" "os" ) //go:embed internal/web/views/* var viewsFS embed.FS func main() { tmpls, err := template.New(""). ParseFS(viewsFS, "internal/web/views/*/*.html", "internal/web/views/*/*/*.html", /* Add more `*` if you have templates that are more nested */ ) if err != nil { log.Fatal(err) } tmpls.ExecuteTemplate(os.Stdout, "pages/home", nil) }
What I did was look into the Glob pattern more and learn how to use it in Go, the Glob pattern in Go is made differently than in any other language.
The above is the detailed content of How to parse an embed.FS template using the template.ParseFS function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...
