skip to content
Alvin Lucillo

Embed file content in Go

/ 1 min read

💻 Tech

If you have an e-mail template, you may not want to store them in a const if that’s too long. If that’s the case, you may want to use Go’s embed package. Suppose you have your templates in templates/email, here’s how you will define it.

Upon compilation, Go will embed the contents of the file you defined in the variable.

import (
	_ "embed"
)

//go:embed templates/email/welcome.html
var WelcomeEmailTemplate string

For more details, check out the documentation and Go By Example