Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"github.com/shu-go/orderedmap"
)
func main() {
type myStruct struct {
V1 string
V2 int
}
m := orderedmap.New[string, myStruct]()
m.Set("ichi", myStruct{
V1: "one",
V2: 1,
})
m.Set("ni", myStruct{
V1: "two",
V2: 2,
})
m.Set("san", myStruct{
V1: "three",
V2: 3,
})
v, found := m.Get("ichi")
fmt.Println(found)
fmt.Println(v)
fmt.Println()
fmt.Println(m.Contains("ni"))
fmt.Println(m.Contains("go"))
}
Output: true {one 1} true false
Example (Marshal) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/shu-go/orderedmap"
)
func main() {
m := orderedmap.New[int, string]()
m.Set(5, "go")
m.Set(9, "ku")
m.Set(6, "ro-")
m.Set(3, "san")
b, _ /*err*/ := json.MarshalIndent(m, "", " ")
fmt.Println(string(b))
}
Output: { "5": "go", "9": "ku", "6": "ro-", "3": "san" }
Example (MarshalYAML) ¶
package main
import (
"fmt"
"gopkg.in/yaml.v3"
"github.com/shu-go/orderedmap"
)
func main() {
m := orderedmap.New[int, string]()
m.Set(5, "go")
m.Set(9, "ku")
m.Set(6, "ro-")
m.Set(3, "san")
b, _ /*err*/ := yaml.Marshal(m)
fmt.Println(string(b))
}
Output: "5": go "9": ku "6": ro- "3": san
Example (Sort) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/shu-go/orderedmap"
)
func main() {
m := orderedmap.New[string, int]()
m.Set("5", 1)
m.Set("9", 2)
m.Set("6", 3)
m.Set("3", 4)
m.Sort(func(i, j string) bool {
return i < j
})
b, _ /*err*/ := json.MarshalIndent(m, "", " ")
fmt.Println(string(b))
}
Output: { "3": 4, "5": 1, "6": 3, "9": 2 }
Example (Unmarshal) ¶
package main
import (
"encoding/json"
"fmt"
"github.com/shu-go/orderedmap"
)
func main() {
m := orderedmap.New[int, string]()
m.Set(999, "dummy")
s := `{
"5": "go",
"9": "ku",
"6": "ro-",
"3": "san"
}`
_ /*err*/ = json.Unmarshal([]byte(s), m)
fmt.Println(m.Get(5))
fmt.Println(m.Get(9))
fmt.Println(m.Get(6))
fmt.Println(m.Get(3))
fmt.Println(m.Get(999))
fmt.Println(m.Get(9999))
}
Output: go true ku true ro- true san true false false
Example (UnmarshalYAML) ¶
package main
import (
"fmt"
"gopkg.in/yaml.v3"
"github.com/shu-go/orderedmap"
)
func main() {
// DOES NOT SUPPORT number keys
m := orderedmap.New[string, string]()
m.Set("999", "dummy")
s := `
"5": go
"9": ku
"6": ro-
"3": san
`
err := yaml.Unmarshal([]byte(s), m)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(m.Get("5"))
fmt.Println(m.Get("9"))
fmt.Println(m.Get("6"))
fmt.Println(m.Get("3"))
fmt.Println(m.Get("999"))
fmt.Println(m.Get("9999"))
}
Output: go true ku true ro- true san true false false
Example (UnmarshalYAML2) ¶
package main
import (
"fmt"
"gopkg.in/yaml.v3"
"github.com/shu-go/orderedmap"
)
func main() {
type aaa struct {
A string
B bool
C []int
}
m := orderedmap.New[string, aaa]()
m.Set("rei", aaa{A: "0", B: false, C: []int{}})
m.Set("ichi", aaa{A: "1", B: true, C: []int{1}})
m.Set("ni", aaa{A: "2", B: false, C: []int{1, 2}})
m.Set("san", aaa{A: "3", B: true, C: []int{1, 2, 3}})
m.Set("shi", aaa{A: "4", B: false, C: []int{1, 2, 3, 4}})
data, err := yaml.Marshal(m)
if err != nil {
fmt.Println("marshal", err)
}
fmt.Println(string(data))
err = yaml.Unmarshal(data, m)
if err != nil {
fmt.Println("unmarshal", err)
}
}
Output: rei: a: "0" b: false c: [] ichi: a: "1" b: true c: - 1 ni: a: "2" b: false c: - 1 - 2 san: a: "3" b: true c: - 1 - 2 - 3 shi: a: "4" b: false c: - 1 - 2 - 3 - 4
Index ¶
- type OrderedMap
- func (m *OrderedMap[K, V]) Contains(key K) bool
- func (m *OrderedMap[K, V]) Delete(key K)
- func (m OrderedMap[K, V]) Format(s fmt.State, verb rune)
- func (m *OrderedMap[K, V]) Get(key K) (V, bool)
- func (m *OrderedMap[K, V]) GetDefault(key K, defvalue V) V
- func (m *OrderedMap[K, V]) Keys() []K
- func (m *OrderedMap[K, V]) Len() int
- func (m *OrderedMap[K, V]) MarshalJSON() ([]byte, error)
- func (m *OrderedMap[K, V]) MarshalYAML() (any, error)
- func (m *OrderedMap[K, V]) PreserveOrder(b bool)
- func (m *OrderedMap[K, V]) Set(key K, value V)
- func (m *OrderedMap[K, V]) Sort(less func(K, K) bool)
- func (m *OrderedMap[K, V]) UnmarshalJSON(b []byte) error
- func (m *OrderedMap[K, V]) UnmarshalYAML(value *yaml.Node) error
- func (m *OrderedMap[K, V]) UnorderedMap() map[K]V
- type SliceHandler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type OrderedMap ¶
type OrderedMap[K comparable, V any] struct { // contains filtered or unexported fields }
func New ¶
func New[K comparable, V any]() *OrderedMap[K, V]
func (*OrderedMap[K, V]) Contains ¶
func (m *OrderedMap[K, V]) Contains(key K) bool
func (*OrderedMap[K, V]) Delete ¶
func (m *OrderedMap[K, V]) Delete(key K)
func (*OrderedMap[K, V]) Get ¶
func (m *OrderedMap[K, V]) Get(key K) (V, bool)
func (*OrderedMap[K, V]) GetDefault ¶
func (m *OrderedMap[K, V]) GetDefault(key K, defvalue V) V
func (*OrderedMap[K, V]) Keys ¶
func (m *OrderedMap[K, V]) Keys() []K
func (*OrderedMap[K, V]) Len ¶
func (m *OrderedMap[K, V]) Len() int
func (*OrderedMap[K, V]) MarshalJSON ¶
func (m *OrderedMap[K, V]) MarshalJSON() ([]byte, error)
func (*OrderedMap[K, V]) MarshalYAML ¶
func (m *OrderedMap[K, V]) MarshalYAML() (any, error)
func (*OrderedMap[K, V]) PreserveOrder ¶
func (m *OrderedMap[K, V]) PreserveOrder(b bool)
func (*OrderedMap[K, V]) Set ¶
func (m *OrderedMap[K, V]) Set(key K, value V)
func (*OrderedMap[K, V]) Sort ¶
func (m *OrderedMap[K, V]) Sort(less func(K, K) bool)
func (*OrderedMap[K, V]) UnmarshalJSON ¶
func (m *OrderedMap[K, V]) UnmarshalJSON(b []byte) error
func (*OrderedMap[K, V]) UnmarshalYAML ¶
func (m *OrderedMap[K, V]) UnmarshalYAML(value *yaml.Node) error
NOT SUPPORTED: number key, nested OrderedMap
func (*OrderedMap[K, V]) UnorderedMap ¶
func (m *OrderedMap[K, V]) UnorderedMap() map[K]V
type SliceHandler ¶
type SliceHandler struct {
// contains filtered or unexported fields
}
func (SliceHandler) Len ¶
func (h SliceHandler) Len() int
func (SliceHandler) Less ¶
func (h SliceHandler) Less(i, j int) bool
func (SliceHandler) Swap ¶
func (h SliceHandler) Swap(i, j int)
Click to show internal directories.
Click to hide internal directories.