← 返回 Skills 市场
samber

Golang Samber Mo

作者 Samuel Berthe · GitHub ↗ · v1.0.3 · MIT-0
cross-platform ✓ 安全检测通过
182
总下载
0
收藏
1
当前安装
3
版本数
在 OpenClaw 中安装
/install golang-samber-mo
功能描述
Monadic types for Golang using samber/mo — Option, Result, Either, Future, IO, Task, and State types for type-safe nullable values, error handling, and funct...
使用说明 (SKILL.md)

Persona: You are a Go engineer bringing functional programming safety to Go. You use monads to make impossible states unrepresentable — nil checks become type constraints, error handling becomes composable pipelines.

Thinking mode: Use ultrathink when designing multi-step Option/Result/Either pipelines. Wrong type choice creates unnecessary wrapping/unwrapping that defeats the purpose of monads.

samber/mo — Monads and Functional Abstractions for Go

Go 1.18+ library providing type-safe monadic types with zero dependencies. Inspired by Scala, Rust, and fp-ts.

Official Resources:

This skill is not exhaustive. Please refer to library documentation and code examples for more information. Context7 can help as a discoverability platform.

go get github.com/samber/mo

For an introduction to functional programming concepts and why monads are valuable in Go, see Monads Guide.

Core Types at a Glance

Type Purpose Think of it as...
Option[T] Value that may be absent Rust's Option, Java's Optional
Result[T] Operation that may fail Rust's Result\x3CT, E>, replaces (T, error)
Either[L, R] Value of one of two types Scala's Either, TypeScript discriminated union
EitherX[L, R] Value of one of X types Scala's Either, TypeScript discriminated union
Future[T] Async value not yet available JavaScript Promise
IO[T] Lazy synchronous side effect Haskell's IO
Task[T] Lazy async computation fp-ts Task
State[S, A] Stateful computation Haskell's State monad

Option[T] — Nullable Values Without nil

Represents a value that is either present (Some) or absent (None). Eliminates nil pointer risks at the type level.

import "github.com/samber/mo"

name := mo.Some("Alice")          // Option[string] with value
empty := mo.None[string]()        // Option[string] without value
fromPtr := mo.PointerToOption(ptr) // nil pointer -> None

// Safe extraction
name.OrElse("Anonymous")  // "Alice"
empty.OrElse("Anonymous")  // "Anonymous"

// Transform if present, skip if absent
upper := name.Map(func(s string) (string, bool) {
    return strings.ToUpper(s), true
})

Key methods: Some, None, Get, MustGet, OrElse, OrEmpty, Map, FlatMap, Match, ForEach, ToPointer, IsPresent, IsAbsent.

Option implements json.Marshaler/Unmarshaler, sql.Scanner, driver.Valuer — use it directly in JSON structs and database models.

For full API reference, see Option Reference.

Result[T] — Error Handling as Values

Represents success (Ok) or failure (Err). Equivalent to Either[error, T] but specialized for Go's error pattern.

// Wrap Go's (value, error) pattern
result := mo.TupleToResult(os.ReadFile("config.yaml"))

// Same-type transform — errors short-circuit automatically
upper := mo.Ok("hello").Map(func(s string) (string, error) {
    return strings.ToUpper(s), nil
})
// Ok("HELLO")

// Extract with fallback
val := upper.OrElse("default")

Go limitation: Direct methods (.Map, .FlatMap) cannot change the type parameter — Result[T].Map returns Result[T], not Result[U]. Go methods cannot introduce new type parameters. For type-changing transforms (e.g. Result[[]byte] to Result[Config]), use sub-package functions or mo.Do:

import "github.com/samber/mo/result"

// Type-changing pipeline: []byte -> Config -> ValidConfig
parsed := result.Pipe2(
    mo.TupleToResult(os.ReadFile("config.yaml")),
    result.Map(func(data []byte) Config { return parseConfig(data) }),
    result.FlatMap(func(cfg Config) mo.Result[ValidConfig] { return validate(cfg) }),
)

Key methods: Ok, Err, Errf, TupleToResult, Try, Get, MustGet, OrElse, Map, FlatMap, MapErr, Match, ForEach, ToEither, IsOk, IsError.

For full API reference, see Result Reference.

Either[L, R] — Discriminated Union of Two Types

Represents a value that is one of two possible types. Unlike Result, neither side implies success or failure — both are valid alternatives.

// API that returns either cached data or fresh data
func fetchUser(id string) mo.Either[CachedUser, FreshUser] {
    if cached, ok := cache.Get(id); ok {
        return mo.Left[CachedUser, FreshUser](cached)
    }
    return mo.Right[CachedUser, FreshUser](db.Fetch(id))
}

// Pattern match
result.Match(
    func(cached CachedUser) mo.Either[CachedUser, FreshUser] { /* use cached */ },
    func(fresh FreshUser) mo.Either[CachedUser, FreshUser] { /* use fresh */ },
)

When to use Either vs Result: Use Result[T] when one path is an error. Use Either[L, R] when both paths are valid alternatives (cached vs fresh, left vs right, strategy A vs B).

Either3[T1, T2, T3], Either4, and Either5 extend this to 3-5 type variants.

For full API reference, see Either Reference.

Do Notation — Imperative Style with Monadic Safety

mo.Do wraps imperative code in a Result, catching panics from MustGet() calls:

result := mo.Do(func() int {
    // MustGet panics on None/Err — Do catches it as Result error
    a := mo.Some(21).MustGet()
    b := mo.Ok(2).MustGet()
    return a * b  // 42
})
// result is Ok(42)

result := mo.Do(func() int {
    val := mo.None[int]().MustGet()  // panics
    return val
})
// result is Err("no such element")

Do notation bridges imperative Go style with monadic safety — write straight-line code, get automatic error propagation.

Pipeline Sub-Packages vs Direct Chaining

samber/mo provides two ways to compose operations:

Direct methods (.Map, .FlatMap) — work when the output type equals the input type:

opt := mo.Some(42)
doubled := opt.Map(func(v int) (int, bool) {
    return v * 2, true
})  // Option[int]

Sub-package functions (option.Map, result.Map) — required when the output type differs from input:

import "github.com/samber/mo/option"

// int -> string type change: use sub-package Map
strOpt := option.Map(func(v int) string {
    return fmt.Sprintf("value: %d", v)
})(mo.Some(42))  // Option[string]

Pipe functions (option.Pipe3, result.Pipe3) — chain multiple type-changing transformations readably:

import "github.com/samber/mo/option"

result := option.Pipe3(
    mo.Some(42),
    option.Map(func(v int) string { return strconv.Itoa(v) }),
    option.Map(func(s string) []byte { return []byte(s) }),
    option.FlatMap(func(b []byte) mo.Option[string] {
        if len(b) > 0 { return mo.Some(string(b)) }
        return mo.None[string]()
    }),
)

Rule of thumb: Use direct methods for same-type transforms. Use sub-package functions + pipes when types change across steps.

For detailed pipeline API reference, see Pipelines Reference.

Common Patterns

JSON API responses with Option

type UserResponse struct {
    Name     string            `json:"name"`
    Nickname mo.Option[string] `json:"nickname"`  // omits null gracefully
    Bio      mo.Option[string] `json:"bio"`
}

Database nullable columns

type User struct {
    ID       int
    Email    string
    Phone    mo.Option[string]  // implements sql.Scanner + driver.Valuer
}

err := row.Scan(&u.ID, &u.Email, &u.Phone)

Wrapping existing Go APIs

// Convert map lookup to Option
func MapGet[K comparable, V any](m map[K]V, key K) mo.Option[V] {
    return mo.TupleToOption(m[key])  // m[key] returns (V, bool)
}

Uniform extraction with Fold

mo.Fold works uniformly across Option, Result, and Either via the Foldable interface:

str := mo.Fold[error, int, string](
    mo.Ok(42),  // works with Option, Result, or Either
    func(v int) string { return fmt.Sprintf("got %d", v) },
    func(err error) string { return "failed" },
)
// "got 42"

Best Practices

  1. Prefer OrElse over MustGetMustGet panics on absent/error values; use it only inside mo.Do blocks where panics are caught, or when you are certain the value exists
  2. Use TupleToResult at API boundaries — convert Go's (T, error) to Result[T] at the boundary, then chain with Map/FlatMap inside your domain logic
  3. Use Result[T] for errors, Either[L, R] for alternatives — Result is specialized for success/failure; Either is for two valid types
  4. Option for nullable fields, not zero valuesOption[string] distinguishes "absent" from "empty string"; use plain string when empty string is a valid value
  5. Chain, don't nestresult.Map(...).FlatMap(...).OrElse(default) reads left-to-right; avoid nested if/else patterns when monadic chaining is cleaner
  6. Use sub-package pipes for multi-step type transformations — when 3+ steps each change the type, option.Pipe3(...) is more readable than nested function calls

For advanced types (Future, IO, Task, State), see Advanced Types Reference.

If you encounter a bug or unexpected behavior in samber/mo, open an issue at \x3Chttps://github.com/samber/mo/issues>.

Cross-References

  • -> See samber/cc-skills-golang@golang-samber-lo skill for functional collection transforms (Map, Filter, Reduce on slices) that compose with mo types
  • -> See samber/cc-skills-golang@golang-error-handling skill for idiomatic Go error handling patterns
  • -> See samber/cc-skills-golang@golang-safety skill for nil-safety and defensive Go coding
  • -> See samber/cc-skills-golang@golang-database skill for database access patterns
  • -> See samber/cc-skills-golang@golang-design-patterns skill for functional options and other Go patterns
安全使用建议
This skill is an instruction-only helper for the samber/mo monadic library and appears coherent and proportionate: it only needs the Go toolchain and provides usage docs and examples. If you want extra assurance, review the upstream GitHub repository linked in the SKILL.md before installing or allowing autonomous execution, and be aware that using the skill may cause the agent to run `go` commands (which may network-fetch packages with `go get`). There are no required credentials or hidden install steps in the provided materials.
功能分析
Type: OpenClaw Skill Name: golang-samber-mo Version: 1.0.3 The skill bundle provides comprehensive and legitimate documentation and instructions for using the 'samber/mo' functional programming library in Go. It includes detailed API references (Option, Result, Either, etc.), best practices, and evaluation cases (evals/evals.json) to ensure the AI agent applies the library correctly. No malicious code, data exfiltration attempts, or harmful prompt injections were found; the allowed tools and bash restrictions are consistent with a standard development environment.
能力评估
Purpose & Capability
Name/description match the content: documentation and usage guidance for the samber/mo Go library. The only required binary is `go`, which is appropriate for a Go library helper.
Instruction Scope
SKILL.md contains examples, API references, and `go get` usage for the library. It does not instruct reading unrelated system files, accessing credentials, or sending data to unexpected endpoints — links point to pkg.go.dev and GitHub, which are expected.
Install Mechanism
There is no install spec and no code files to execute. This instruction-only skill has minimal disk/write surface and does not download arbitrary archives.
Credentials
The skill requests no environment variables, credentials, or config paths — consistent with a documentation/usage skill for a library.
Persistence & Privilege
always:false and no install actions; the skill does not request permanent presence or modify other skills. Note: user-invocable is false but model invocation is allowed (default), which is normal for skills.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install golang-samber-mo
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /golang-samber-mo 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.3
- Updated version metadata to 1.0.3 in SKILL.md. - Added AskUserQuestion to the allowed-tools list. - No technical or functionality changes to skill logic—documentation only.
v1.0.1
Version 1.0.1 - Updated SKILL.md metadata to "version: 1.0.1". - Added evals/evals.json file. - No functional changes to core logic; documentation and metadata improvements only.
v0.1.0
Initial release introducing monadic types and functional programming patterns for Go via samber/mo: - Provides Option, Result, Either, Future, IO, Task, and State types for type-safe nullability and composable error handling. - Supports both direct method chaining and sub-package pipelines for functional workflows, with clear guidance on when to use each. - Includes documentation and concise examples for primary monadic types, pattern matching, Do notation, and pipeline composition. - Compatible with Go 1.18+ and projects importing github.com/samber/mo. - Designed for use in safety-focused Go codebases and by Claude Code or similar coding agents.
元数据
Slug golang-samber-mo
版本 1.0.3
许可证 MIT-0
累计安装 1
当前安装数 1
历史版本数 3
常见问题

Golang Samber Mo 是什么?

Monadic types for Golang using samber/mo — Option, Result, Either, Future, IO, Task, and State types for type-safe nullable values, error handling, and funct... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 182 次。

如何安装 Golang Samber Mo?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install golang-samber-mo」即可一键安装,无需额外配置。

Golang Samber Mo 是免费的吗?

是的,Golang Samber Mo 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Golang Samber Mo 支持哪些平台?

Golang Samber Mo 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Golang Samber Mo?

由 Samuel Berthe(@samber)开发并维护,当前版本 v1.0.3。

💬 留言讨论