Playground
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/orderedmapdemo/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186411 max_deposit = "5000000ugnot"
orderedmapdemo.gno
// Package orderedmapdemo is a small gnoweb demo of the insertion-ordered map
// provided by the [p/moul/x/daily/orderedmap](/p/nym-moultest123/x/daily/orderedmap/v0)
// library: it shows that order survives updates and deletes.
//
// It contains no map logic of its own. Stateless, so Render is deterministic —
// which is precisely what the library is for.
package orderedmapdemo
import (
"strconv"
"strings"
"gno.land/p/nym-moultest123/x/daily/orderedmap/v0"
)
// Render renders the demo for gnoweb.
func Render(path string) string {
var b strings.Builder
b.WriteString("# Ordered Map\n\n")
b.WriteString("A map that remembers insertion order, demoing the ")
b.WriteString("[`p/moul/x/daily/orderedmap`](/p/nym-moultest123/x/daily/orderedmap/v0) library.\n\n")
o := orderedmap.New()
for _, kv := range [][2]string{{"delta", "4"}, {"alpha", "1"}, {"charlie", "3"}, {"bravo", "2"}} {
o.Set(kv[0], kv[1])
}
b.WriteString("## Inserted\n\n")
b.WriteString("`delta, alpha, charlie, bravo` → order kept as inserted, **not** sorted:\n\n")
b.WriteString(table(o))
o.Set("alpha", "99")
b.WriteString("\n## After `Set(\"alpha\", \"99\")`\n\n")
b.WriteString("Updating keeps the original position — insertion order means *first* insertion:\n\n")
b.WriteString(table(o))
o.Delete("charlie")
o.Set("charlie", "new")
b.WriteString("\n## After deleting and re-adding `charlie`\n\n")
b.WriteString("Re-inserting is a **new** insertion, so it goes last:\n\n")
b.WriteString(table(o))
b.WriteString("\n> gno map iteration order is unspecified. A realm that ranges over a ")
b.WriteString("built-in map to build its page can emit a different page every call — ")
b.WriteString("a consensus bug, not a cosmetic one. This is the fix.\n")
return b.String()
}
func table(o *orderedmap.OrderedMap) string {
var b strings.Builder
b.WriteString("| # | key | value |\n|---|---|---|\n")
i := 0
o.Iterate(func(k, v string) bool {
i++
b.WriteString("| ")
b.WriteString(strconv.Itoa(i))
b.WriteString(" | `")
b.WriteString(k)
b.WriteString("` | `")
b.WriteString(v)
b.WriteString("` |\n")
return false
})
return b.String()
}
render_example_test.gno
package orderedmapdemo
// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # Ordered Map
//
// A map that remembers insertion order, demoing the [`p/moul/x/daily/orderedmap`](/p/nym-moultest123/x/daily/orderedmap/v0) library.
//
// ## Inserted
//
// `delta, alpha, charlie, bravo` → order kept as inserted, **not** sorted:
//
// | # | key | value |
// |---|---|---|
// | 1 | `delta` | `4` |
// | 2 | `alpha` | `1` |
// | 3 | `charlie` | `3` |
// | 4 | `bravo` | `2` |
//
// ## After `Set("alpha", "99")`
//
// Updating keeps the original position — insertion order means *first* insertion:
//
// | # | key | value |
// |---|---|---|
// | 1 | `delta` | `4` |
// | 2 | `alpha` | `99` |
// | 3 | `charlie` | `3` |
// | 4 | `bravo` | `2` |
//
// ## After deleting and re-adding `charlie`
//
// Re-inserting is a **new** insertion, so it goes last:
//
// | # | key | value |
// |---|---|---|
// | 1 | `delta` | `4` |
// | 2 | `alpha` | `99` |
// | 3 | `bravo` | `2` |
// | 4 | `charlie` | `new` |
//
// > gno map iteration order is unspecified. A realm that ranges over a built-in map to build its page can emit a different page every call — a consensus bug, not a cosmetic one. This is the fix.
}