The two-element composite of go-composites.
A Pair is a fixed, heterogeneous grouping of two values — the natural
key/value entry. It is interface-first and never goes nil: the Null-Object
Pair honours the whole Interface.
go get github.com/go-composites/pairpackage main
import (
"fmt"
Pair "github.com/go-composites/pair/src"
)
func main() {
entry := Pair.New("answer", 42)
fmt.Println(entry.ToGoString()) // (answer, 42)
fmt.Println(entry.First()) // answer
fmt.Println(entry.Second()) // 42
fmt.Println(entry.Equal(Pair.New("answer", 42))) // true
fmt.Println(entry.ToArray().Len()) // 2
}New(first, second interface{}) Interface— a Pair holding the two values.Null() Interface— the Null-ObjectPair.
| method | returns | notes |
|---|---|---|
First() |
interface{} |
the first slot |
Second() |
interface{} |
the second slot |
Equal(other) |
Go bool |
both slots reflect.DeepEqual (false against a null Pair) |
ToArray() |
Array.Interface |
a two-element [first, second] Array |
ToGoString() |
Go string |
"(first, second)" ("(null)" for the Null-Object) |
IsNull() |
Go bool |
true only for the Null-Object |
Null() returns the never-nil Null-Object Pair: its slots are nil, it
Equals nothing (not even another null Pair), ToArray() is the empty
Array, and ToGoString() renders "(null)". IsNull() reports true for
it.
BSD-3-Clause © the go-composites/pair authors.
