Compilar un solo binario con plantillas
Usando //go:embed (recomendado)
Desde Go 1.16, la biblioteca estándar soporta la incrustación de archivos directamente en el binario con la directiva //go:embed. No se necesitan dependencias de terceros.
package main
import ( "embed" "html/template" "net/http"
"github.com/gin-gonic/gin")
//go:embed templates/*var templateFS embed.FS
func main() { router := gin.Default()
t := template.Must(template.ParseFS(templateFS, "templates/*.tmpl")) router.SetHTMLTemplate(t)
router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "index.tmpl", nil) })
router.Run(":8080")}Usando un paquete de terceros
También puedes usar un paquete de terceros como go-assets para incrustar plantillas en el binario.
func main() { router := gin.New()
t, err := loadTemplate() if err != nil { panic(err) } router.SetHTMLTemplate(t)
router.GET("/", func(c *gin.Context) { c.HTML(http.StatusOK, "/html/index.tmpl", nil) }) router.Run(":8080")}
// loadTemplate loads templates embedded by go-assets-builderfunc loadTemplate() (*template.Template, error) { t := template.New("") for name, file := range Assets.Files { if file.IsDir() || !strings.HasSuffix(name, ".tmpl") { continue } h, err := io.ReadAll(file) if err != nil { return nil, err } t, err = t.New(name).Parse(string(h)) if err != nil { return nil, err } } return t, nil}Consulta un ejemplo completo en el directorio assets-in-binary/example01.