### Installing go-ordered-map/v2 with Go Get Source: https://github.com/wk8/go-ordered-map/blob/master/README.md This snippet demonstrates how to install the `go-ordered-map/v2` library using the `go get` command. This command fetches the latest version of the module and adds it to your Go module cache, making it available for use in your projects. ```Bash go get -u github.com/wk8/go-ordered-map/v2 ``` -------------------------------- ### Initializing go-ordered-map/v2 with Initial Data in Go Source: https://github.com/wk8/go-ordered-map/blob/master/README.md This example demonstrates how to create an `OrderedMap` and populate it with initial key-value pairs directly during instantiation. It uses the `orderedmap.WithInitialData` option, allowing for convenient setup of the map with predefined entries. ```Go om := orderedmap.New[int, string](orderedmap.WithInitialData[int, string]( orderedmap.Pair[int, string]{ Key: 12, Value: "foo" }, orderedmap.Pair[int, string]{ Key: 28, Value: "bar" } )) ``` -------------------------------- ### Iterating Ordered Map Elements in Go Source: https://github.com/wk8/go-ordered-map/blob/master/README.md This snippet demonstrates how to iterate over an `orderedmap` in Go using `FromOldest()` to get key-value pairs in insertion order and `KeysNewest()` to get keys in reverse insertion order. It shows the setup of an `orderedmap` and then two different iteration patterns, printing the results to the console. ```Go om := orderedmap.New[int, string]() om.Set(1, "foo") om.Set(2, "bar") om.Set(3, "baz") for k, v := range om.FromOldest() { fmt.Printf("%d => %s\n", k, v) } // prints: // 1 => foo // 2 => bar // 3 => baz for k := range om.KeysNewest() { fmt.Printf("%d\n", k) } // prints: // 3 // 2 // 1 ``` -------------------------------- ### Basic Usage of go-ordered-map/v2 in Go Source: https://github.com/wk8/go-ordered-map/blob/master/README.md This example showcases the fundamental operations of `go-ordered-map/v2`, including setting key-value pairs, retrieving values, iterating from oldest to newest, iterating over the newest pairs with a break condition, filtering elements, and using the new iteration syntax. It demonstrates how to manage string keys and values. ```Go package main import ( "fmt" "strings" "github.com/wk8/go-ordered-map/v2" ) func main() { om := orderedmap.New[string, string]() om.Set("foo", "bar") om.Set("bar", "baz") om.Set("coucou", "toi") fmt.Println(om.Get("foo")) // => "bar", true fmt.Println(om.Get("i dont exist")) // => "", false // iterating pairs from oldest to newest: for pair := om.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%s => %s\n", pair.Key, pair.Value) } // prints: // foo => bar // bar => baz // coucou => toi // 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 } } // prints: // coucou => toi // bar => baz // removing all pairs which do not have an "o" in their key om.Filter(func(key, value string) bool { return strings.Contains(key, "o") }) // new iteration syntax for key, value := range om.FromOldest() { fmt.Printf("%s => %s\n", key, value) }// prints: // foo => bar // coucou => toi } ``` -------------------------------- ### Creating New Ordered Map from Iterator in Go Source: https://github.com/wk8/go-ordered-map/blob/master/README.md This example illustrates the use of the `orderedmap.From()` convenience function to create a new `OrderedMap` instance from an existing iterator. It initializes an `orderedmap`, then uses its `FromOldest()` iterator to populate a new `orderedmap2`, demonstrating how to effectively copy or transform map contents. ```Go om := orderedmap.New[int, string]() om.Set(1, "foo") om.Set(2, "bar") om.Set(3, "baz") om2 := orderedmap.From(om.FromOldest()) for k, v := range om2.FromOldest() { fmt.Printf("%d => %s\n", k, v) } // prints: // 1 => foo // 2 => bar // 3 => baz ``` -------------------------------- ### YAML Serialization and Deserialization with go-ordered-map/v2 in Go Source: https://github.com/wk8/go-ordered-map/blob/master/README.md This example illustrates how to serialize an `OrderedMap` to YAML and deserialize YAML back into an `OrderedMap`, ensuring order preservation. It leverages the `gopkg.in/yaml.v3` package, providing a straightforward method for YAML data handling. ```Go // serialization data, err := yaml.Marshal(om) ... // deserialization om := orderedmap.New[string, string]() // or orderedmap.New[int, any](), or any type you expect err := yaml.Unmarshal(data, &om) ... ``` -------------------------------- ### Using go-ordered-map/v2 with Custom Struct Types in Go Source: https://github.com/wk8/go-ordered-map/blob/master/README.md This example demonstrates how to use `go-ordered-map/v2` with custom struct types as values and integer keys. It shows setting and retrieving a custom struct, and iterating through the map to access the struct's fields, highlighting the flexibility of the ordered map with generic types. ```Go type myStruct struct { payload string } func main() { om := orderedmap.New[int, *myStruct]() om.Set(12, &myStruct{"foo"}) om.Set(1, &myStruct{"bar"}) value, present := om.Get(12) if !present { panic("should be there!") } fmt.Println(value.payload) // => foo for pair := om.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%d => %s\n", pair.Key, pair.Value.payload) } // prints: // 12 => foo // 1 => bar } ``` -------------------------------- ### Initializing go-ordered-map/v2 with Capacity Hint in Go Source: https://github.com/wk8/go-ordered-map/blob/master/README.md This snippet illustrates how to initialize an `OrderedMap` with a predefined capacity hint. Similar to `make(map[K]V, capacity)`, this can optimize performance by pre-allocating memory for a specified number of elements, reducing reallocations as items are added. ```Go om := orderedmap.New[int, *myStruct](28) ``` -------------------------------- ### JSON Serialization and Deserialization with go-ordered-map/v2 in Go Source: https://github.com/wk8/go-ordered-map/blob/master/README.md This snippet shows how to serialize an `OrderedMap` to JSON and deserialize JSON back into an `OrderedMap`, preserving the insertion order. It uses the standard `encoding/json` package, demonstrating seamless integration for data persistence and exchange. ```Go // serialization data, err := json.Marshal(om) ... // deserialization om := orderedmap.New[string, string]() // or orderedmap.New[int, any](), or any type you expect err := json.Unmarshal(data, &om) ... ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.