samber/cc-skills-golang / skills/golang-samber-mo
golang-samber-mo
VerifiedMonadic types for Golang using samber/mo — Option, Result, Either, Future, IO, Task, and State types for type-safe nullable values, error handling, and functional composition with pipeline sub-packages. Apply when using or adopting samber/mo, when the codebase imports `github.com/samber/mo`, or when considering functional programming patterns as a safety design for Golang. Not for nil-safety and zero-value design without this library (→ See `samber/cc-skills-golang@golang-safety` skill), nor for native error wrapping with fmt.Errorf, errors.Is and errors.As (→ See `samber/cc-skills-golang@golang-error-handling` skill).
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 11.5 KB |
| evals/evals.json | config | 23.2 KB |
| references/advanced-types.md | markdown | 8.6 KB |
| references/either.md | markdown | 5.5 KB |
| references/monads-guide.md | markdown | 7.5 KB |
| references/option.md | markdown | 5.2 KB |
| references/pipelines.md | markdown | 5.9 KB |
| references/result.md | markdown | 4.3 KB |
references/result.md
View on GitHub# Result[T] API Reference
## Table of Contents
- [Constructors](#constructors)
- [Do Notation](#do-notation)
- [Query Methods](#query-methods)
- [Value Extraction](#value-extraction)
- [Transformations](#transformations)
- [Map — transform successful value](#map--transform-successful-value)
- [MapValue — transform without error possibility](#mapvalue--transform-without-error-possibility)
- [MapErr — transform error state](#maperr--transform-error-state)
- [FlatMap — chain Results](#flatmap--chain-results)
- [Match — handle both cases](#match--handle-both-cases)
- [ForEach — side effect on success](#foreach--side-effect-on-success)
- [Conversion](#conversion)
- [JSON Serialization](#json-serialization)
## Constructors
| Function | Description |
| --- | --- |
| `mo.Ok[T](value T)` | Creates a successful Result |
| `mo.Err[T](err error)` | Creates a failed Result |
| `mo.Errf[T](format string, a ...any)` | Creates failed Result with formatted error message |
| `mo.TupleToResult[T](value T, err error)` | Converts Go's (T, error) tuple — Ok if err is nil, Err otherwise |
| `mo.Try[T](f func() (T, error))` | Executes function, wraps result — Ok on success, Err on error |
### Do Notation
```go
result := mo.Do(func() int {
a := mo.Ok(10).MustGet() // panics if Err -> caught by Do
b := mo.Ok(32).MustGet()
return a + b
})
// Ok(42)
```
`mo.Do` executes a closure and catches any panic from `MustGet()` calls, converting them to `Err`. This enables imperative-style code with monadic error propagation.
## Query Methods
| Method | Returns | Description |
| --- | --- | --- |
| `IsOk()` | `bool` | True if Result is successful |
| `IsError()` | `bool` | True if Result is a failure |
| `Error()` | `error` | Returns the error, or nil if Ok |
| `Get()` | `(T, error)` | Returns value and error (Go-style) |
| `MustGet()` | `T` | Returns value or panics — use only inside `mo.Do` |
## Value Extraction
| Method | Returns | Description |
| -------------------- | ------- | ------------------------------ |
| `OrElse(fallback T)` | `T` | Value if Ok, fallback if Err |
| `OrEmpty()` | `T` | Value if Ok, zero value if Err |
## Transformations
### Map — transform successful value
```go
result := mo.Ok(42).
Map(func(v int) (int, error) {
return v * 2, nil
})
// Ok(84)
// Errors short-circuit
result := mo.Err[int](errors.New("fail")).
Map(func(v int) (int, error) {
return v * 2, nil // never called
})
// Err("fail")
```
**Go limitation:** Result.Map takes `func(T) (T, error)` — the input and output types must be the same `T`. Returning a non-nil error converts Ok to Err. To change the type (e.g. `Result[[]byte]` to `Result[Config]`), use sub-package `result.Map` or `mo.Do` notation — see [Pipelines Reference](./pipelines.md).
### MapValue — transform without error possibility
```go
result := mo.Ok(42).MapValue(func(v int) int {
return v * 2
})
// Ok(84) — no error possible in the mapper
```
### MapErr — transform error state
```go
result := mo.Err[int](errors.New("fail")).
MapErr(func(err error) (int, error) {
return 0, fmt.Errorf("wrapped: %w", err)
})
// Err("wrapped: fail")
```
### FlatMap — chain Results
```go
func parseAge(s string) mo.Result[int] {
v, err := strconv.Atoi(s)
return mo.TupleToResult(v, err)
}
func validateAge(age int) mo.Result[int] {
if age < 0 || age > 150 {
return mo.Errf[int]("invalid age: %d", age)
}
return mo.Ok(age)
}
result := parseAge("25").FlatMap(func(age int) mo.Result[int] {
return validateAge(age)
})
// Ok(25)
```
### Match — handle both cases
```go
result.Match(
func(v int) (int, error) {
fmt.Println("Success:", v)
return v, nil
},
func(err error) (int, error) {
fmt.Println("Error:", err)
return 0, err
},
)
```
### ForEach — side effect on success
```go
result.ForEach(func(v int) {
fmt.Println("Got:", v) // only executes if Ok
})
```
## Conversion
```go
either := result.ToEither() // Either[error, T]
// Ok(42) -> Right(42)
// Err(e) -> Left(e)
```
## JSON Serialization
Result marshals to JSON-RPC format:
```go
// Ok(42) marshals to:
{"result": 42}
// Err("fail") marshals to:
{"error": {"message": "fail"}}
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 323k | Updated | Claude CodeCodex | Verified | Install |
| xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |