Skip to content

Utility functions for working with optional values represented as pointers in Go

License

Notifications You must be signed in to change notification settings

s2-streamstore/optr

Repository files navigation

optr

Package optr provides utility functions for working with optional values represented as pointers in Go. It simplifies common operations like mapping, cloning, and handling default values.

Features

  • Create pointers from non-pointer values.

    value := optr.Some(42)
    fmt.Println(*value) // Output: 42
  • Create new values from existing pointers.

    original := optr.Some(42)
    copy := optr.Cloned(original)
    fmt.Println(*copy) // Output: 42
    fmt.Println(copy != original) // Output: true
  • Apply transformations to pointer values.

    value := optr.Some(42)
    result := optr.Map(value, func(v int) string { return fmt.Sprintf("Value: %d", v) })
    fmt.Println(*result) // Output: "Value: 42"
  • Handle default values and fallbacks with ease.

    value := optr.Or(nil, func() (int, bool) {
      return 42, true
    })
    fmt.Println(*value) // Output: 42
  • Support for functions that may fail, returning errors where applicable.

    value, err := optr.TryAnd(Some(42), func(v int) (string, bool, error) {
      if v > 40 {
        return "Greater than 40", true, nil
      }
      return "", false, fmt.Errorf("value is not greater than 40")
    })
    if err != nil {
      fmt.Println("Error:", err)
    } else {
      fmt.Println(*value) // Output: "Greater than 40"
    }

Installation

Install the package using go get:

go get github.com/s2-streamstore/optr@latest

Documentation

Head over to pkg.go.dev for detailed documentation and package reference.

Feedback

We use Github Issues to track feature requests and issues with the package. If you wish to provide feedback, report a bug or request a feature, feel free to open a Github issue.

Contributing

Developers are welcome to submit Pull Requests on the repository. If there is no tracking issue for the bug or feature request corresponding to the PR, we encourage you to open one for discussion before submitting the PR.

Reach out to us

Join our Discord server. We would love to hear from you.

You can also email us at [email protected].

License

This project is licensed under the Apache-2.0 License.

About

Utility functions for working with optional values represented as pointers in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages