Skip to content

spenserblack/go-defaultmap

Repository files navigation

defaultmap

Go Reference CI codecov

A map that supports default values.

Basic Example

import (
	"fmt"

	"github.com/spenserblack/go-defaultmap"
)

m := defaultmap.NewMap[string](func() string { return "I'm the default!" })
m.Insert("exists", "Hello, World!")

fmt.Println(m.Get("exists")) // Hello, World!
fmt.Println(m.Get("doesn't exist")) // I'm the default!
fmt.Println(m.GetOr("also doesn't exist", "I'm a one-time default!")) // I'm a one-time default!