Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

mo

> 编程语言
Open source

Monads and popular FP abstractions, powered by Go 1.18+ Generics (Option, Result, Either...)

3.4K stars0 likes0 views
WebsiteGitHub

About

Monads and popular FP abstractions, powered by Go 1.18+ Generics (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, ..

GitHub Issues· 17 open

View all on GitHub
  • #37

    Type transformations in Map

    Updated Jun 2, 2026
  • #21

    Weird function signature on `(mo.Option).Map`

    breaking-changeUpdated Jun 2, 2026
  • #102

    Result[T] return signature in functions `result.Match` and `resultMapErr`

    breaking-changeUpdated Jun 2, 2026
  • #66

    Transforming return type when using map

    breaking-changeUpdated Jun 2, 2026
  • #72

    Option JSON serialization/deserialization does not handle the null case correctly

    Updated Sep 22, 2025
  • #20

    Decoding BSON values

    Updated Aug 10, 2025
  • #6

    Add Helper function that would work like Either.Match but would return another type

    Updated Jun 10, 2025
  • #50

    v1.13.0 changed supported generic types for Option and driver.Valuer interface

    Updated Jul 25, 2024
  • #48

    Feature Request: (or How-To) about call a method on Foldable when "T", "U" inherit from same interface

    Updated Jul 8, 2024
  • #11

    Translating types

    Updated Jun 22, 2024

Highlights

  • •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
  • •Option[T] (Maybe)
  • •Result[T]
  • •Either[A, B]
  • •EitherX[T1, ..., TX] (With X between 3 and 5)
  • •Future[T]
  • •IOEither[T]

> Tags

Gocatseitherfpfunctional

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

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