package module
Version: v0.0.0-...-c57d5ad Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2020 License: MIT Imports: 2 Imported by: 3


Go pattern matching

Mentioned in Awesome Go Go Report Card Build Status codecov GoDoc LICENSE

It's just another implementation of pattern matching in Go. I have been inspired by Python pattern matching, that's why I wanted to try writing something similar in Go :) For now the following matching are implemented :

  • Simple types (like int, int64, float, float64, bool..).
  • Struct type.
  • Slices (with HEAD, TAIL, OneOf patterns).
  • Dictionary (with ANY, OneOf pattern).
  • Regexp.
  • Additional custom matching (ability to add special matching for some, structs for example).


Fibonacci example:

func fib(n int) int {
	_, res := match.Match(n).
		When(1, 1).
		When(2, 1).
		When(match.ANY, func() int { return fib(n-1) + fib(n-2) }).

	return res.(int)

Simple types:

isMatched, mr := match.Match(42).
                When(42, 10).
// isMatched - true, mr - 10

With Structs:

  • Simple check value by type
val := TestStruct{1}

isMatched, _ := Match(val).
    When(func(TestStruct) {},  1).
  • Check value by type and condition
val := TestStruct{1}

isMatched, _ := Match(val).
	When(func(ts TestStruct) bool { return ts.value == 42 }, 1).
	When(func(ts AnotherStruct) bool { return ts.stringValue == "hello" }, 2).

With Maps:

isMatched, mr := match.Match(map[string]int{
                	"rsc": 3711,
                	"r":   2138,
                	"gri": 1908,
                	"adg": 912,
                	"rsc": 3711,
                	"r":   2138,
                	"gri": 1908,
                	"adg": match.ANY,
            	}, true).

With Slices:

isMatched, mr := match.Match([]int{1, 2, 3, 4, 5, 6}).
            	When([]interface{}{match.HEAD, 3, match.OneOf(3, 4), 5, 6}, 125).

With regexps:

isMatched, mr := match.Match("gophergopher").
            	When("gophergopher", func() interface{} { return true }).

Without result:

func main() {
	When(42, func() { fmt.Println("You found the answer to life, universe and everything!") }).
	When(ANY, func() { fmt.Println("No.. It's not an answer.") }).


Just go get this repository in the following way:

go get

Full example

package main

import (

func main() {
    isMatched, mr := match.Match([]int{1, 2, 3}).
        When(42, false).
        When([]interface{}{match.HEAD, 2, 3}, true).

    if isMatched {




View Source
const (
	// ANY is the pattern which allows any value.
	ANY matchKey = 0
	// HEAD is the pattern for start element of silce.
	HEAD matchKey = 1
	// TAIL is the pattern for end element(s) of slice.
	TAIL matchKey = 2


This section is empty.


func OneOf

func OneOf(items ...interface{}) oneOfContainer

OneOf defines the pattern where at least one item matches.

func RegisterMatcher

func RegisterMatcher(pattern PatternChecker)

RegisterMatcher register custom pattern.


type MatchItem

type MatchItem struct {
	// contains filtered or unexported fields

MatchItem defines a matched item value.

type Matcher

type Matcher struct {
	// contains filtered or unexported fields

Matcher struct

func Match

func Match(val interface{}) *Matcher

Match function takes a value for matching and

func (*Matcher) Result

func (matcher *Matcher) Result() (bool, interface{})

Result returns the result value of matching process.

func (*Matcher) When

func (matcher *Matcher) When(val interface{}, fun interface{}) *Matcher

When function adds new pattern for checking matching. If pattern matched with value the func will be called.

type PatternChecker

type PatternChecker func(pattern interface{}, value interface{}) bool

PatternChecker is func for checking pattern.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL