百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

mo

> 编程语言
开源

Monad 和流行的 FP 抽象,由 Go 1.18+ 通用类 (Option、Result、Either 等) 提供支持

3.4K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Monad 和流行的 FP 抽象,由 Go 1.18+ 通用类 (Option、Result、Either 等) 提供支持

mo - Monads

samber/mo brings monads and popular FP abstractions to Go projects. samber/mo uses the recent Go 1.18+ Generics.

Inspired by:

  • Scala
  • Rust
  • FP-TS

See also:

  • samber/ro: Reactive Programming for Go: declarative and composable API for event-driven applications
  • samber/lo: A Lodash-style Go library based on Go 1.18+ Generics
  • samber/do: A dependency injection toolkit based on Go 1.18+ Generics
  • samber/cc-skills-golang: AI Agent Skills for Golang


Why this name?

I love short name for such utility library. This name is similar to "Monad Go" and no Go package uses this name.

Features

We currently support the following data types:

  • Option[T] (Maybe)
  • Result[T]
  • Either[A, B]
  • EitherX[T1, ..., TX] (With X between 3 and 5)
  • Future[T]
  • IO[T]
  • IOEither[T]
  • Task[T]
  • TaskEither[T]
  • State[S, A]

Install

go get github.com/samber/mo@v1

# AI Agent Skill
npx skills add https://github.com/samber/cc-skills-golang --skill golang-samber-mo

This library is v1 and follows SemVer strictly.

No breaking changes will be made to exported APIs before v2.0.0.

This library has no dependencies except the Go std lib.

Quick start

You can import mo using:

import (
    "github.com/samber/mo"
)

Quick example using the option sub-package Pipe3 to compose transformations:

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

out := option.Pipe3(
    mo.Some(21),
    option.Map(func(v int) int { return v * 2 }),
    option.FlatMap(func(v int) mo.Option[int] { return mo.None[int]() }),
    option.Map(func(v int) int { return v + 21 }),
)
// out == None[int]

Then use one of the helpers below:

…

More examples in documentation.

Tips for lazy developers

I cannot recommend it, but in case you are too lazy for repeating mo. everywhere, you can import the entire library into the namespace.

import (
    . "github.com/samber/mo"
)

I take no responsibility on this junk.

Documentation and examples

GoDoc: https://godoc.org/github.com/samber/mo

Option[T any]

Option is a container for an optional value of type T. If value exists, Option is of type Some. If the value is absent, Option is of type None.

Implements:

  • mo.Foldable[T, U]

Constructors:

  • mo.Some() doc - play
  • mo.None() doc - play
  • mo.TupleToOption() doc - play
  • mo.EmptyableToOption() doc - play
  • mo.PointerToOption() doc - play

Methods:

  • .IsPresent() doc - play
  • .IsSome() doc - play
  • .IsAbsent() doc - play
  • .IsNone() doc - play
  • .Size() doc - play
  • .Get() doc - play
  • .MustGet() doc - play
  • .OrElse() doc - play
  • .OrEmpty() doc - play
  • .ToPointer() doc - play
  • .ForEach() doc
  • .Match() doc - play
  • .Map() doc - play
  • .MapNone() doc - play
  • .MapValue() doc
  • .FlatMap() doc - play
  • .MarshalJSON() doc
  • .UnmarshalJSON() doc
  • .MarshalText() doc
  • .UnmarshalText() doc
  • .MarshalBinary() doc
  • .UnmarshalBinary() doc
  • .GobEncode() doc
  • .GobDecode() doc
  • .Scan() doc
  • .Value() doc

Other:

  • mo.Fold[T, U, R any](f Foldable[T, U], successFunc func(U) R, failureFunc func(T) R) R doc

Sub-package option (transformations and pipes):

  • option.Map()() doc
  • option.FlatMap()() doc
  • option.Match()() doc
  • option.FlatMatch()() doc
  • option.Pipe1..Pipe10() docs

Result[T any]

Result respresent a result of an action having one of the following output: success or failure. An instance of Result is an instance of either Ok or Err. It could be compared to Either[error, T].

Implements:

  • mo.Foldable[T, U]

Constructors:

  • mo.Ok() doc - play
  • mo.Err() doc - play
  • mo.Errf() doc - play
  • mo.TupleToResult() doc - play
  • mo.Try() doc - play

Methods:

  • .IsOk() doc - play
  • .IsError() doc - play
  • .Error() doc - play
  • .Get() doc - play
  • .MustGet() doc - play
  • .OrElse() doc - play
  • .OrEmpty() doc - play
  • .ToEither() doc - play
  • .ForEach() doc - play
  • .Match() doc - play
  • .Map() doc - play
  • .MapValue() doc - play
  • .MapErr() doc - play
  • .FlatMap() doc - play

Other:

  • mo.Fold[T, U, R any](f Foldable[T, U], successFunc func(U) R, failureFunc func(T) R) R doc
  • mo.Do[T any](fn func() T) (result mo.Result[T]) doc

Sub-package result (transformations and pipes):

  • result.Map()() doc
  • result.FlatMap()() doc
  • result.Match()() doc
  • result.FlatMatch()() doc
  • result.Pipe1..Pipe10() docs

Either[L any, R any]

Either represents a value of 2 possible types. An instance of Either is an instance of either A or B.

Implements:

  • mo.Foldable[T, U]

Constructors:

  • mo.Left() doc
  • mo.Right() doc

Methods:

  • .IsLeft() doc
  • .IsRight() doc
  • .Left() doc
  • .Right() doc
  • .MustLeft() doc
  • .MustRight() doc
  • .Unpack() doc
  • .LeftOrElse() doc
  • .RightOrElse() doc
  • .LeftOrEmpty() doc
  • .RightOrEmpty() doc
  • .Swap() doc
  • .ForEach() doc
  • .Match() doc
  • .MapLeft() doc
  • .MapRight() doc

Other:

  • mo.Fold[T, U, R any](f Foldable[T, U], successFunc func(U) R, failureFunc func(T) R) R doc

Sub-package either (transformations and pipes):

  • either.MapLeft()() doc
  • either.MapRight()() doc
  • either.Match()() doc
  • either.Swap()() doc
  • either.Pipe1..Pipe10() docs

EitherX[T1, ..

Issues· 17 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Gocatseitherfpfunctional

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言