encoding

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package encoding provides utilities for encoding and decoding data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirExists

func DirExists(path string) bool

DirExists checks if a directory exists at the given path.

func EnsureDir

func EnsureDir(path string) error

EnsureDir creates a directory and all parent directories if they don't exist. Uses 0755 permissions.

func EnsureParentDir

func EnsureParentDir(filePath string) error

EnsureParentDir ensures the parent directory of a file path exists.

func FileExists

func FileExists(path string) bool

FileExists checks if a file exists at the given path.

func LoadJSON

func LoadJSON[T any](path string) (*T, error)

LoadJSON reads a JSON file and unmarshals it into the provided value. Returns nil, nil if the file does not exist. Returns an error for other file access or parsing issues.

func MustLoadJSON

func MustLoadJSON[T any](path string) *T

MustLoadJSON is like LoadJSON but panics on error. Use only when the file is expected to exist and be valid.

func ParseJSON

func ParseJSON[T any](data []byte) (*T, error)

ParseJSON unmarshals JSON data into the provided type. Returns an error if parsing fails.

func ReadFile

func ReadFile(path string) ([]byte, error)

ReadFile reads the entire contents of a file. Returns nil, nil if the file does not exist.

func SaveJSON

func SaveJSON[T any](path string, value T) error

SaveJSON marshals the value to JSON and writes it to the specified path. Creates parent directories if they don't exist. Uses 0600 permissions for the file.

func ToJSON

func ToJSON[T any](value T) ([]byte, error)

ToJSON marshals a value to JSON bytes. Returns an error if marshaling fails.

func ToJSONIndent

func ToJSONIndent[T any](value T) ([]byte, error)

ToJSONIndent marshals a value to indented JSON bytes. Returns an error if marshaling fails.

func WriteFile

func WriteFile(path string, data []byte, perm os.FileMode) error

WriteFile writes data to a file with the specified permissions. Creates parent directories if they don't exist.

func WriteFileSecure

func WriteFileSecure(path string, data []byte) error

WriteFileSecure writes data to a file with 0600 permissions (owner read/write only). Use for sensitive data like tokens or configs.

Types

This section is empty.

Jump to

Keyboard shortcuts

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