Skip to content

muzig/go-feature

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Feature

Go Version License

A type-safe Promise-like asynchronous programming library for Go, providing chainable operations with error handling and timeout control.

Features

  • Type Safe: Uses Go generics for compile-time type checking
  • Chainable: Supports Then(), Catch(), Finally() method chaining
  • Timeout Control: Built-in context timeout support
  • Concurrent Operations: FeatureAll and FeatureRace methods
  • Lightweight: Goroutine-based implementation

Installation

go get github.com/muzig/go-feature

Quick Start

Basic Usage

package main

import (
    "fmt"
    "go-feature/feature"
    "time"
)

func main() {
    // Create an async operation
    f := feature.NewFeature(func(resolve func(string), reject func(error)) {
        time.Sleep(1 * time.Second)
        resolve("Hello from Feature!")
    })

    // Wait for result
    result, err := f.Await()
    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }
    fmt.Printf("Result: %s\n", result)
}

Method Chaining

f := feature.NewFeature(func(resolve func(int), reject func(error)) {
    time.Sleep(500 * time.Millisecond)
    resolve(10)
})

result, err := f.Then(func(x int) int {
    return x * 2
}).Then(func(x int) int {
    return x + 5
}).Catch(func(err error) int {
    fmt.Printf("Caught error: %v\n", err)
    return 0
}).Finally(func() {
    fmt.Println("Operation completed")
}).Await()

Error Handling

f := feature.NewFeature(func(resolve func(string), reject func(error)) {
    reject(fmt.Errorf("something went wrong"))
})

result, err := f.Catch(func(err error) string {
    fmt.Printf("Caught error: %v\n", err)
    return "fallback value"
}).Await()

Concurrent Operations

// Wait for all operations to complete
features := []*feature.Feature[int]{
    feature.NewFeature(func(resolve func(int), reject func(error)) {
        time.Sleep(200 * time.Millisecond)
        resolve(1)
    }),
    feature.NewFeature(func(resolve func(int), reject func(error)) {
        time.Sleep(300 * time.Millisecond)
        resolve(2)
    }),
}

allResults, err := feature.FeatureAll(features...).Await()

// Race operation - return first completed result
raceResult, err := feature.FeatureRace(features...).Await()

Timeout Control

ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

slowFeature := feature.NewFeature(func(resolve func(string), reject func(error)) {
    time.Sleep(2 * time.Second)
    resolve("This will timeout")
})

_, err := slowFeature.AwaitWithContext(ctx)
if err != nil {
    fmt.Printf("Timeout error: %v\n", err)
}

API Reference

Core Methods

  • NewFeature[T](executor func(resolve func(T), reject func(error))) *Feature[T] - Create new Feature
  • FeatureResolve[T](value T) *Feature[T] - Create resolved Feature
  • FeatureReject[T](err error) *Feature[T] - Create rejected Feature
  • Then(handler func(T) T) *Feature[T] - Chain success handler
  • Catch(handler func(error) T) *Feature[T] - Chain error handler
  • Finally(handler func()) *Feature[T] - Always execute handler
  • Await() (T, error) - Wait for completion
  • AwaitWithContext(ctx context.Context) (T, error) - Wait with timeout
  • FeatureAll[T](features ...*Feature[T]) *Feature[[]T] - Wait for all
  • FeatureRace[T](features ...*Feature[T]) *Feature[T] - Return first completed

Testing

go test ./...        # Run tests
go test -cover ./... # Run with coverage

Project Structure

go-feature/
├── feature/
│   ├── feature.go      # Core implementation
│   └── feature_test.go # Tests
├── main.go             # Example usage
└── README.md

Requirements

  • Go 1.25.0+

License

MIT License - see LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/NewFeature)
  3. Commit changes (git commit -m 'Add NewFeature')
  4. Push to branch (git push origin feature/NewFeature)
  5. Open Pull Request

About

A type-safe Promise-like asynchronous programming library for Go, providing chainable operations with error handling and timeout control.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages