# Go API

The Go package's constructor, options, methods, and types, and where it differs from the JavaScript package.

The package is `github.com/rjrodger/patrun/go`, imported as `patrun`. Its version is the constant `patrun.Version`, 0.4.0 for this page.

## `func New(opts ...Options) *Patrun`

Makes a router. Pass at most one `Options`. `New` panics if `Options.Order` names a key twice, or names the empty string.

`Router` is another name for `Patrun`, the name the TypeScript package and the definition give it. It is an alias, so a `*patrun.Router` is a `*patrun.Patrun`.

## `Options`

```go
type Options struct {
	Gex      bool     // glob matching on values that have a * or ? in them
	Interval bool     // matching of numbers by interval
	Order    []string // the keys that come first in the key order
}
```

`Order` names the keys that come first in the router's key order, in the order given. Every other key comes after them, in the order of its text. `New` copies the slice.

## Methods of `*Patrun`

| Method | Does |
|---|---|
| `Add(pattern map[string]string, data *string) *Patrun` | Stores `data` under `pattern`, and returns the router. With `nil` data, clears the pattern as `Remove` does. Panics if the pattern is `nil` or has the empty string as a key. |
| `Find(query map[string]string) (*string, bool)` | The data of the pattern that best matches, and `true`; or `nil` and `false`. Keys no pattern has are ignored. Nothing matches a `nil` query. |
| `FindExact(query map[string]string) (*string, bool)` | As `Find`, taking only a pattern that has every key of the query. |
| `Collect(query map[string]string) []string` | The data of every pattern that matches, in the [order of a list](https://rjrodger.github.io/patrun/reference/matching.html#the-order-of-a-list). |
| `CollectExact(query map[string]string) []string` | The data of every pattern that matches and has every key of the query, in the order of `Find`. |
| `Get(pattern map[string]string) (*string, bool)` | The data stored at exactly the pattern, and whether it holds any. Does not panic. |
| `List(part map[string]string) []ListEntry` | Every pattern that has every pair of `part`; `nil` lists them all. |
| `Remove(pattern map[string]string)` | Clears the data stored at exactly the pattern. Panics as `Add` does. |
| `String() string` | The patterns and their data, one pattern to a line. It makes a `*Patrun` an `fmt.Stringer`. |
| `ToJSON() string` | The tree as JSON, with the members the JavaScript `toJSON` gives. |
| `Top() *keymap` | The root of the tree, for debugging. |

The rules that decide a match are those of [How a match is chosen](https://rjrodger.github.io/patrun/reference/matching.html), and the patterns a method takes are in [What patterns and queries hold](https://rjrodger.github.io/patrun/reference/values.html#in-go).

## `ListEntry`

```go
type ListEntry struct {
	Pattern map[string]string
	Data    string
}
```

`Entry` is another name for `ListEntry`, an alias.

## Matchers

A router asks a slice of `Matcher`s to recognise pattern values that are not exact. The two it has are `GexMatcher`, which `Options.Gex` turns on, and `IntervalMatcher`, which `Options.Interval` turns on.

```go
type Matcher interface {
	Make(key, fix string) (MatchValue, bool)
}

type MatchValue interface {
	Match(val string) bool
	Same(other MatchValue) bool
	Kind() string
	Fix() string
	Keymap() *keymap
	SetKeymap(km *keymap)
}
```

## Where Go differs from JavaScript

| JavaScript | Go |
|---|---|
| A pattern's values are strings, numbers, or booleans | A pattern is a `map[string]string`: format a number with `strconv` |
| Data is anything | Data is a `*string`: to attach more, store a key into your own table |
| `null` or `undefined` data clears a pattern | `nil` data clears a pattern |
| A customiser can change patterns and data | No customiser |
| `toString`, and `inspect` | `String`, and `ToJSON` |
| `add` throws a `TypeError` | `Add` panics |

The answers are the same: a router of each, given the same patterns, gives the same data for the same query. Go orders keys as Go compares strings, which for UTF-8 is by code point, as JavaScript's order is.
