Involved Source Filesjson.go Package orderedmap implements an ordered map, i.e. a map that also keeps track of
the order in which keys were inserted.
All operations are constant-time.
Github repo: https://github.com/wk8/go-ordered-mapyaml.go
Code Examples
{
om := orderedmap.New[string, string](3)
om.Set("foo", "bar")
om.Set("bar", "baz")
om.Set("coucou", "toi")
fmt.Println("## Get operations: ##")
fmt.Println(om.Get("foo"))
fmt.Println(om.Get("i dont exist"))
fmt.Println(om.Value("coucou"))
fmt.Println("## Iterating over pairs from oldest to newest: ##")
for pair := om.Oldest(); pair != nil; pair = pair.Next() {
fmt.Printf("%s => %s\n", pair.Key, pair.Value)
}
fmt.Println("## Iterating over the 2 newest pairs: ##")
i := 0
for pair := om.Newest(); pair != nil; pair = pair.Prev() {
fmt.Printf("%s => %s\n", pair.Key, pair.Value)
i++
if i >= 2 {
break
}
}
fmt.Println("## JSON serialization: ##")
data, err := json.Marshal(om)
if err != nil {
panic(err)
}
fmt.Println(string(data))
fmt.Println("## JSON deserialization: ##")
om2 := orderedmap.New[string, string]()
if err := json.Unmarshal(data, &om2); err != nil {
panic(err)
}
fmt.Println(om2.Oldest().Key)
}
Type Parameters:
K: comparable KeyNotFoundError may be returned by functions in this package when they're called with keys that are not present
in the map.MissingKeyK(*KeyNotFoundError[K]) Error() string
*KeyNotFoundError : error
Type Parameters:
K: comparable
V: any AddPairs allows setting multiple pairs at a time. It's equivalent to calling
Set on each pair sequentially. Delete removes the key-value pair, and returns what `Get` would have returned
on that key prior to the call to `Delete`. Get looks for the given key, and returns the value associated with it,
or V's nil value if not found. The boolean it returns says whether the key is present in the map. GetAndMoveToBack combines Get and MoveToBack in the same call. If an error is returned,
it will be a KeyNotFoundError. GetAndMoveToFront combines Get and MoveToFront in the same call. If an error is returned,
it will be a KeyNotFoundError. GetPair looks for the given key, and returns the pair associated with it,
or nil if not found. The Pair struct can then be used to iterate over the ordered map
from that point, either forward or backward. Len returns the length of the ordered map. Load is an alias for Get, mostly to present an API similar to `sync.Map`'s. MarshalJSON implements the json.Marshaler interface. MarshalYAML implements the yaml.Marshaler interface. MoveAfter moves the value associated with key to its new position after the one associated with markKey.
Returns an error iff key or markKey are not present in the map. If an error is returned,
it will be a KeyNotFoundError. MoveBefore moves the value associated with key to its new position before the one associated with markKey.
Returns an error iff key or markKey are not present in the map. If an error is returned,
it will be a KeyNotFoundError. MoveToBack moves the value associated with key to the back of the ordered map,
i.e. makes it the newest pair in the map.
Returns an error iff key is not present in the map. If an error is returned,
it will be a KeyNotFoundError. MoveToFront moves the value associated with key to the front of the ordered map,
i.e. makes it the oldest pair in the map.
Returns an error iff key is not present in the map. If an error is returned,
it will be a KeyNotFoundError. Newest returns a pointer to the newest pair. It's meant to be used to iterate on the ordered map's
pairs from the newest to the oldest, e.g.:
for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) } Oldest returns a pointer to the oldest pair. It's meant to be used to iterate on the ordered map's
pairs from the oldest to the newest, e.g.:
for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) } Set sets the key-value pair, and returns what `Get` would have returned
on that key prior to the call to `Set`. Store is an alias for Set, mostly to present an API similar to `sync.Map`'s. UnmarshalJSON implements the json.Unmarshaler interface. UnmarshalYAML implements the yaml.Unmarshaler interface. Value returns the value associated with the given key or the zero value.
*OrderedMap : github.com/goccy/go-json.Marshaler
*OrderedMap : github.com/goccy/go-json.Unmarshaler
*OrderedMap : encoding/json.Marshaler
*OrderedMap : encoding/json.Unmarshaler
*OrderedMap : gopkg.in/yaml.v3.Marshaler
*OrderedMap : gopkg.in/yaml.v3.Unmarshaler
func New[K, V](options ...any) *OrderedMap[K, V]
func github.com/invopop/jsonschema.NewProperties() *OrderedMap[string, *jsonschema.Schema]
Type Parameters:
K: comparable
V: any New creates a new OrderedMap.
options can either be one or several InitOption[K, V], or a single integer,
which is then interpreted as a capacity hint, à la make(map[K]V, capacity).
Type Parameters:
K: comparable
V: any WithCapacity allows giving a capacity hint for the map, akin to the standard make(map[K]V, capacity).
Type Parameters:
K: comparable
V: any WithInitialData allows passing in initial data for the map.
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.