examples

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package examples. examples demonstrates practical usage patterns for the enhanced Result type.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserNotFound   = errors.New("user not found")
	ErrInvalidEmail   = errors.New("invalid email format")
	ErrDatabaseDown   = errors.New("database unavailable")
	ErrCacheMiss      = errors.New("cache miss")
	ErrAPITimeout     = errors.New("API request timeout")
	ErrUnauthorized   = errors.New("unauthorized access")
	ErrInvalidInput   = errors.New("invalid input")
	ErrConfigNotFound = errors.New("configuration not found")
)

Functions

func ExecuteTransaction

func ExecuteTransaction(db *sql.DB, userID int, amount float64) (res result.Result[string])

ExecuteTransaction demonstrates error handling in database transactions.

func FetchDataMultiLayer

func FetchDataMultiLayer(id int) (res result.Result[string])

FetchDataMultiLayer demonstrates cascading fallbacks through multiple data sources. Tries: Memory -> Local Cache -> Database -> Remote API -> Default

func FetchWithRetry

func FetchWithRetry(url string, maxRetries int) (res result.Result[[]byte])

FetchWithRetry demonstrates implementing retry logic with error handlers.

func FetchWithTimeout

func FetchWithTimeout(ctx context.Context, url string) (res result.Result[[]byte])

FetchWithTimeout demonstrates context cancellation handling.

func FindUserWithFallback

func FindUserWithFallback(db *sql.DB, cache Cache, userID int) (res result.Result[User])

FindUserWithFallback demonstrates using BubbleUp() with CatchWith for cache fallback. If database fails, it automatically tries the cache.

func HandleGetUser

func HandleGetUser(db *sql.DB) http.HandlerFunc

HandleGetUser demonstrates using CatchErr to adapt to traditional (value, error) signatures. Useful for HTTP handlers and interface implementations.

func LoadConfiguration

func LoadConfiguration(configPath string) (res result.Result[Config])

LoadConfiguration demonstrates using Fallback for default values.

func MigrateToResult

func MigrateToResult(db *sql.DB, userID int) (res result.Result[User])

MigrateToResult shows gradual migration from traditional error handling.

func ProcessOrderPipeline

func ProcessOrderPipeline(db *sql.DB, orderID int) (res result.Result[string])

ProcessOrderPipeline demonstrates chaining multiple operations with BubbleUp(). Each step can fail and will automatically propagate errors.

func ResultStyleWithAndThen

func ResultStyleWithAndThen(db *sql.DB, orderID int) result.Result[string]

ResultStyleWithAndThen shows the functional chaining pattern.

func ResultStyleWithTry

func ResultStyleWithTry(db *sql.DB, orderID int) (res result.Result[string])

ResultStyleWithTry shows the new BubbleUp() pattern (recommended for sequential operations).

func TraditionalStyle

func TraditionalStyle(db *sql.DB, orderID int) (string, error)

TraditionalStyle shows standard Go error handling.

func ValidateUserInput

func ValidateUserInput(email, password, username string) (res result.Result[User])

ValidateUserInput demonstrates chaining validations with BubbleUp().

Types

type Cache

type Cache interface {
	GetUser(id int) result.Result[User]
	SetUser(user User) result.Result[bool]
}

type Config

type Config struct {
	DatabaseURL string
	CacheURL    string
	APITimeout  time.Duration
}

type Order

type Order struct {
	ID         int       `json:"id"`
	UserID     int       `json:"user_id"`
	TotalPrice float64   `json:"total_price"`
	Status     string    `json:"status"`
	CreatedAt  time.Time `json:"created_at"`
}

type User

type User struct {
	ID        int       `json:"id"`
	Email     string    `json:"email"`
	Name      string    `json:"name"`
	CreatedAt time.Time `json:"created_at"`
}

Jump to

Keyboard shortcuts

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