filesystem

package
v0.29.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2026 License: AGPL-3.0 Imports: 1 Imported by: 0

README

Filesystem Utilities

The filesystem package provides utilities for working with embedded file systems in Go applications.

Features

  • EmbeddedFileToBytes: Read files from embedded filesystem as byte slices
  • EmbeddedFileToString: Read files from embedded filesystem as strings

Installation

import "github.com/dracory/base/filesystem"

Usage

Reading Embedded Files
//go:embed templates/*.html
var templateFS embed.FS

// Read as bytes
bytes, err := filesystem.EmbeddedFileToBytes(templateFS, "templates/index.html")
if err != nil {
    log.Fatal(err)
}

// Read as string
content, err := filesystem.EmbeddedFileToString(templateFS, "templates/index.html")
if err != nil {
    log.Fatal(err)
}
fmt.Println(content)

Functions

EmbeddedFileToBytes(embeddedFileSystem embed.FS, path string) ([]byte, error)

Reads a file from the embedded filesystem and returns its content as a byte slice.

Parameters:

  • embeddedFileSystem: The embedded filesystem (embed.FS)
  • path: Path to the file within the embedded filesystem

Returns:

  • []byte: File content as bytes
  • error: Error if file cannot be read
EmbeddedFileToString(embeddedFileSystem embed.FS, path string) (string, error)

Reads a file from the embedded filesystem and returns its content as a string.

Parameters:

  • embeddedFileSystem: The embedded filesystem (embed.FS)
  • path: Path to the file within the embedded filesystem

Returns:

  • string: File content as string
  • error: Error if file cannot be read

Examples

See the test files for comprehensive examples of how to use these utilities with embedded filesystems.

Testing

The package includes comprehensive tests using embedded test data:

go test ./filesystem

Dependencies

  • Go 1.16+ (for embed support)
  • No external dependencies

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EmbeddedFileToBytes

func EmbeddedFileToBytes(embeddedFileSystem embed.FS, path string) ([]byte, error)

EmbeddedFileToBytes reads a file from the embedded filesystem and returns its content as bytes

func EmbeddedFileToString

func EmbeddedFileToString(embeddedFileSystem embed.FS, path string) (string, error)

EmbeddedFileToString reads a file from the embedded filesystem and returns its content as a string

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL