Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Embed Directive

This Go program uses the embed package to embed files and directories into the compiled binary. Let's go through the code with inline comments:

// Importing the embed package.
import (
	"embed"
)

//go:embed folder/single_file.txt
var fileString string // Embedding the content of a single text file as a string.

//go:embed folder/single_file.txt
var fileByte []byte // Embedding the content of a single text file as a byte slice.

//go:embed folder/single_file.txt
//go:embed folder/*.hash
var folder embed.FS // Embedding a directory and its contents.

func main() {
	// Printing the content of the embedded string.
	print(fileString)
	// Printing the content of the embedded byte slice.
	print(string(fileByte))

	// Reading and printing the content of a specific file ("folder/file1.hash") from the embedded directory.
	content1, _ := folder.ReadFile("folder/file1.hash")
	print(string(content1))

	// Reading and printing the content of another file ("folder/file2.hash") from the embedded directory.
	content2, _ := folder.ReadFile("folder/file2.hash")
	print(string(content2))
}

// print function for convenience.
func print(s string) {
	println(s)
}

Explanation:

  1. Embedding a Single File as a String and Byte Slice:

    • fileString is a string variable that embeds the content of the file at "folder/single_file.txt" as a string.

    • fileByte is a byte slice variable that embeds the same content as a byte slice.

  2. Embedding a Directory and Its Contents:

    • folder is an embed.FS variable that embeds the entire "folder" directory and its contents.

  3. Reading and Printing Embedded Content:

    • The main function prints the content of the embedded string, byte slice, and reads and prints the content of specific files within the embedded directory.

  4. Print Function:

    • A simple print function is defined for convenience to print the embedded content.

This program demonstrates how to embed files and directories into a Go program using the embed package.

PreviousTemporary Files and DirectoriesNextTesting and Benchmarking

Last updated 1 year ago

Was this helpful?