trit implements three-valued (ternary) logic in Go. A Trit is an int8
with three states — False (any negative number), Unknown (0) and True
(any positive number) — and the full set of ternary logic operators (Not,
And, Or, Xor, Nand, Nor, Nxor, implication, equivalence and more).
The key property is that the zero value is Unknown, so an uninitialized
Trit is already meaningful. That is what makes it useful wherever a "maybe" or
"not set" state matters — config merging, partial updates, nullable database
columns, logic circuits — where a plain bool cannot tell "false" apart from
"unset".
- Three-valued logic operations (True, False, Unknown); zero value is Unknown.
- Safe bool conversions with explicit
Unknownhandling. - Serialization: JSON (Unknown →
null), text, anddatabase/sql(Unknown →NULL). ParseTrit,Compare(ordering False < Unknown < True), andDefault.- Slice aggregates:
All,Any,None,Known,Consensus,Majority(plusiter.Seqforms). - Zero allocations for basic operations; property and fuzz tested.
go get github.com/goloop/trit/v2import "github.com/goloop/trit/v2"Requires Go 1.24 or newer.
package main
import (
"fmt"
"github.com/goloop/trit/v2"
)
func main() {
t1, t2 := trit.True, trit.False
fmt.Println(t1.IsTrue()) // true
fmt.Println(t1.And(t2)) // False
fmt.Println(t1.Or(t2)) // True
fmt.Println(t1.Xor(t2)) // True
// Resolve the Unknown state.
t3 := trit.Unknown
t3.Default(trit.True) // set only if currently Unknown
fmt.Println(t3) // True
}Why three-valued logic? A bool config field cannot distinguish "explicitly
false" from "not provided". A trit.Trit field can — an unset field stays
Unknown, so you can apply a default without clobbering an explicit choice:
type Config struct {
Enabled trit.Trit // Unknown until the caller sets it
}
func (s *Service) Configure(c Config) {
if !c.Enabled.IsUnknown() {
s.enabled = c.Enabled
}
trit.Default(&s.enabled, true) // default only if still unset
}- Full reference, truth tables and recipes: DOC.md · DOC.UK.md
- Package API: pkg.go.dev/github.com/goloop/trit/v2
- Changes between versions: CHANGELOG.md
Contributions are welcome. Please run go test ./..., go vet ./... and
gofmt -l . before submitting a pull request.
trit is released under the MIT License. See LICENSE.