Go //go:embed Directive

beginner
8 min

Go //go:embed Directive

Welcome to our deep dive into the //go:embed directive in the Go programming language! This powerful tool is a game-changer for embedding files directly into your Go source code, making it easier to manage resources for your projects. 🎯

What is the //go:embed directive?

The //go:embed directive is a Go-specific method to embed files directly into your Go source code. This directive is used along with the embed package to manage files like images, CSS, JavaScript, and other resources required for your projects. 📝

Why use //go:embed?

Using //go:embed has several benefits:

  1. Simplifies project management: By embedding resources directly into your Go code, you can avoid keeping separate files for images, CSS, or JavaScript and ensure they're always available with your code.
  2. Portability: Embedded resources are included in the compiled binary, making your project more portable as you don't need to worry about external dependencies.
  3. Efficiency: Having all resources compiled with your code means you can run your project without worrying about additional file dependencies.

How to use //go:embed?

To get started with //go:embed, follow these simple steps:

  1. First, import the embed package:
go
import ( "embed" "io" "net/http" )
  1. Next, define a variable and use the //go:embed directive to specify the files you want to embed:
go
//go:embed *.css *.js images/* var assets embedding.FS

In this example, we're embedding all CSS files, JavaScript files, and images located in the 'images' folder.

  1. To use the embedded resources, access the files using the Open() function and pass the desired file path:
go
cssFile, err := assets.Open("main.css") if err != nil { log.Fatal(err) }
  1. Read the file content and write it to an io.Reader:
go
cssBytes, err := io.ReadAll(cssFile) if err != nil { log.Fatal(err) }

Now you can use cssBytes to manipulate the embedded CSS file content as needed.

Real-world example

Let's build a simple web server that serves an embedded CSS file and a static image:

go
package main import ( "embed" "fmt" "io" "net/http" ) //go:embed static/* var assets embedding.FS func main() { http.HandleFunc("/", serveContent) http.HandleFunc("/styles.css", serveCss) http.HandleFunc("/logo.png", serveImage) http.ListenAndServe(":8080", nil) } func serveContent(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "<!DOCTYPE html>") fmt.Fprint(w, "<html lang='en'>") fmt.Fprint(w, "<head>") fmt.Fprint(w, "<meta charset='UTF-8'>") fmt.Fprint(w, "<title>Go Embed Example</title>") fmt.Fprint(w, "</head>") fmt.Fprint(w, "<body>") fmt.Fprint(w, "<img src='/logo.png'>") fmt.Fprint(w, "</body>") fmt.Fprint(w, "</html>") } func serveCss(w http.ResponseWriter, r *http.Request) { cssFile, err := assets.Open("styles.css") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } io.Copy(w, cssFile) } func serveImage(w http.ResponseWriter, r *http.Request) { imgFile, err := assets.Open("logo.png") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } io.Copy(w, imgFile) }

Now, if you run this server, you'll see a simple HTML page with an image embedded in the source. 💡

Quiz

Happy coding, and stay tuned for more Go goodness! 🤓