codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

41 / 72 topics
27Standard Library Overview28IO Package29Net Package30HTTP Package31JSON Package32XML Package33Time Package34OS Package35Fmt Package36Math Package37Regexp Package38Log Package39Flag Package40Context Package41Embed Package
Tutorials/Go (Golang)/Embed Package
🐹Go (Golang)

Embed Package

Updated 2026-04-20
3 min read

Embed Package

The embed package in Go (Golang) provides a way to embed static files directly into your executable, making it easier to distribute and deploy applications. This feature is particularly useful for including configuration files, templates, images, or any other static assets without the need for external dependencies.

Introduction to the Embed Package

Before diving into how to use the embed package, let's understand its purpose and benefits:

  • Simplicity: No need to manage separate directories for static files.
  • Deployment: Easier deployment as all resources are bundled with the executable.
  • Performance: Faster access to embedded files since they are part of the binary.

Basic Usage

To use the embed package, you first need to import it:

import "embed"

Embedding a Single File

Here's how you can embed a single file:

//go:embed hello.txt
var helloFile string

In this example, hello.txt is embedded into the variable helloFile. The content of hello.txt will be stored as a string in helloFile.

Embedding Multiple Files

To embed multiple files, you can use a slice:

//go:embed hello.txt world.txt
var files []string

This will store the contents of both hello.txt and world.txt in the files slice.

Embedding Directories

You can also embed entire directories using the embed.FS type:

//go:embed assets/*
var assetFS embed.FS

In this case, all files within the assets directory will be embedded into assetFS.

Accessing Embedded Files

Reading a Single File

To read the content of an embedded file, you can use the strings package:

package main

import (
	"fmt"
	"strings"

	_ "embed"
)

//go:embed hello.txt
var helloFile string

func main() {
	fmt.Println(strings.TrimSpace(helloFile))
}

Reading Multiple Files

For multiple files, you can iterate over the slice and read each file:

package main

import (
	"fmt"
	"strings"

	_ "embed"
)

//go:embed hello.txt world.txt
var files []string

func main() {
	for _, file := range files {
		fmt.Println(strings.TrimSpace(file))
	}
}

Reading Files from an Embedded Directory

To read files from an embedded directory, use the embed.FS type:

package main

import (
	"embed"
	"fmt"
	"io/fs"
)

//go:embed assets/*
var assetFS embed.FS

func main() {
	dirEntries, _ := fs.ReadDir(assetFS, "assets")
	for _, entry := range dirEntries {
		if !entry.IsDir() {
			fileContent, _ := fs.ReadFile(assetFS, entry.Name())
			fmt.Println(string(fileContent))
		}
	}
}

Best Practices

  1. Use Specific Paths: Always specify the exact paths of files or directories to avoid unintended inclusions.
  2. Avoid Large Files: Embedding large files can increase the size of your binary significantly. Use this feature judiciously.
  3. Version Control: Ensure that embedded files are included in version control if they are necessary for the application's functionality.
  4. Security Considerations: Be cautious with embedding sensitive information, such as API keys or passwords.

Real-World Example

Let's create a simple web server that serves static HTML content from an embedded directory:

package main

import (
	"embed"
	"fmt"
	"net/http"

	_ "embed"
)

//go:embed templates/*
var templateFS embed.FS

func main() {
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(templateFS))))
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, "templates/index.html")
	})

	fmt.Println("Starting server at port 8080")
	if err := http.ListenAndServe(":8080", nil); err != nil {
		fmt.Printf("Failed to start server: %v\n", err)
	}
}

In this example:

  • The templates directory is embedded into the application.
  • A static file server is set up to serve files from the embedded directory.
  • The root URL serves the index.html file.

Conclusion

The embed package in Go provides a powerful and flexible way to include static files directly into your executable. By following best practices and understanding how to access and use embedded files, you can streamline your application's deployment and management process.


PreviousContext PackageNext Third-Party Libraries Overview

Recommended Gear

Context PackageThird-Party Libraries Overview