Documentation ¶
Index ¶
- type Getter
- type Loader
- type Map
- func (o *Map) Clear()
- func (o *Map) Copy() MapI
- func (o *Map) Delete(key string)
- func (o *Map) Equals(i MapI) bool
- func (o *Map) Get(key string) (val interface{})
- func (o *Map) Has(key string) (exists bool)
- func (o *Map) IsNil() bool
- func (o *Map) Keys() (keys []string)
- func (o *Map) Len() (l int)
- func (o *Map) Load(key string) (val interface{}, ok bool)
- func (o *Map) LoadBool(key string) (val bool, ok bool)
- func (o *Map) LoadFloat64(key string) (val float64, ok bool)
- func (o *Map) LoadInt(key string) (val int, ok bool)
- func (o *Map) LoadString(key string) (val string, ok bool)
- func (o *Map) MarshalBinary() ([]byte, error)
- func (o *Map) MarshalJSON() (out []byte, err error)
- func (o *Map) Merge(i MapI)
- func (o *Map) MergeMap(m map[string]interface{})
- func (o *Map) Range(f func(key string, value interface{}) bool)
- func (o *Map) Set(key string, val interface{})
- func (o *Map) String() string
- func (o *Map) UnmarshalBinary(data []byte) (err error)
- func (o *Map) UnmarshalJSON(in []byte) (err error)
- func (o *Map) Values() (vals []interface{})
- type MapI
- type SafeMap
- func (o *SafeMap) Clear()
- func (o *SafeMap) Copy() MapI
- func (o *SafeMap) Delete(key string)
- func (o *SafeMap) Equals(i MapI) bool
- func (o *SafeMap) Get(key string) (val interface{})
- func (o *SafeMap) Has(key string) (exists bool)
- func (o *SafeMap) IsNil() bool
- func (o *SafeMap) Keys() (keys []string)
- func (o *SafeMap) Len() (l int)
- func (o *SafeMap) Load(key string) (val interface{}, ok bool)
- func (o *SafeMap) LoadBool(key string) (val bool, ok bool)
- func (o *SafeMap) LoadFloat64(key string) (val float64, ok bool)
- func (o *SafeMap) LoadInt(key string) (val int, ok bool)
- func (o *SafeMap) LoadString(key string) (val string, ok bool)
- func (o *SafeMap) MarshalBinary() ([]byte, error)
- func (o *SafeMap) MarshalJSON() (out []byte, err error)
- func (o *SafeMap) Merge(i MapI)
- func (o *SafeMap) MergeMap(m map[string]interface{})
- func (o *SafeMap) Range(f func(key string, value interface{}) bool)
- func (o *SafeMap) Set(key string, val interface{})
- func (o *SafeMap) String() string
- func (o *SafeMap) UnmarshalBinary(data []byte) (err error)
- func (o *SafeMap) UnmarshalJSON(in []byte) (err error)
- func (o *SafeMap) Values() (vals []interface{})
- type SafeSliceMap
- func (o *SafeSliceMap) Clear()
- func (o *SafeSliceMap) Copy() *SafeSliceMap
- func (o *SafeSliceMap) Delete(key string)
- func (o *SafeSliceMap) Equals(i MapI) bool
- func (o *SafeSliceMap) Get(key string) (val interface{})
- func (o *SafeSliceMap) GetAt(position int) (val interface{})
- func (o *SafeSliceMap) GetKeyAt(position int) (key string)
- func (o *SafeSliceMap) Has(key string) (ok bool)
- func (o *SafeSliceMap) IsNil() bool
- func (o *SafeSliceMap) Keys() (keys []string)
- func (o *SafeSliceMap) Len() int
- func (o *SafeSliceMap) Load(key string) (val interface{}, ok bool)
- func (o *SafeSliceMap) LoadBool(key string) (val bool, ok bool)
- func (o *SafeSliceMap) LoadFloat64(key string) (val float64, ok bool)
- func (o *SafeSliceMap) LoadInt(key string) (val int, ok bool)
- func (o *SafeSliceMap) LoadString(key string) (val string, ok bool)
- func (o *SafeSliceMap) MarshalBinary() (data []byte, err error)
- func (o *SafeSliceMap) MarshalJSON() (data []byte, err error)
- func (o *SafeSliceMap) Merge(i MapI)
- func (o *SafeSliceMap) MergeMap(m map[string]interface{})
- func (o *SafeSliceMap) Range(f func(key string, value interface{}) bool)
- func (o *SafeSliceMap) Set(key string, val interface{})
- func (o *SafeSliceMap) SetAt(index int, key string, val interface{})
- func (o *SafeSliceMap) SetSortFunc(f func(key1, key2 string, val1, val2 interface{}) bool) *SafeSliceMap
- func (o *SafeSliceMap) SortByKeys() *SafeSliceMap
- func (o *SafeSliceMap) String() string
- func (o *SafeSliceMap) UnmarshalBinary(data []byte) (err error)
- func (o *SafeSliceMap) UnmarshalJSON(data []byte) (err error)
- func (o *SafeSliceMap) Values() (vals []interface{})
- type SafeStringMap
- func (o *SafeStringMap) Clear()
- func (o *SafeStringMap) Copy() StringMapI
- func (o *SafeStringMap) Delete(key string)
- func (o *SafeStringMap) Equals(i StringMapI) bool
- func (o *SafeStringMap) Get(key string) (val string)
- func (o *SafeStringMap) Has(key string) (exists bool)
- func (o *SafeStringMap) Is(key string, val string) (is bool)
- func (o *SafeStringMap) IsNil() bool
- func (o *SafeStringMap) Keys() (keys []string)
- func (o *SafeStringMap) Len() (l int)
- func (o *SafeStringMap) Load(key string) (val string, ok bool)
- func (o *SafeStringMap) MarshalBinary() ([]byte, error)
- func (o *SafeStringMap) MarshalJSON() (out []byte, err error)
- func (o *SafeStringMap) Merge(i StringMapI)
- func (o *SafeStringMap) MergeMap(m map[string]string)
- func (o *SafeStringMap) Range(f func(key string, value string) bool)
- func (o *SafeStringMap) Set(key string, val string)
- func (o *SafeStringMap) SetChanged(key string, val string) (changed bool)
- func (o *SafeStringMap) String() string
- func (o *SafeStringMap) UnmarshalBinary(data []byte) (err error)
- func (o *SafeStringMap) UnmarshalJSON(in []byte) (err error)
- func (o *SafeStringMap) Values() (vals []string)
- type SafeStringSliceMap
- func (o *SafeStringSliceMap) Clear()
- func (o *SafeStringSliceMap) Copy() *SafeStringSliceMap
- func (o *SafeStringSliceMap) Delete(key string)
- func (o *SafeStringSliceMap) Equals(i StringMapI) bool
- func (o *SafeStringSliceMap) Get(key string) (val string)
- func (o *SafeStringSliceMap) GetAt(position int) (val string)
- func (o *SafeStringSliceMap) GetKeyAt(position int) (key string)
- func (o *SafeStringSliceMap) Has(key string) (ok bool)
- func (o *SafeStringSliceMap) Is(key string, val string) (is bool)
- func (o *SafeStringSliceMap) IsNil() bool
- func (o *SafeStringSliceMap) Join(glue string) string
- func (o *SafeStringSliceMap) Keys() (keys []string)
- func (o *SafeStringSliceMap) Len() int
- func (o *SafeStringSliceMap) Load(key string) (val string, ok bool)
- func (o *SafeStringSliceMap) MarshalBinary() (data []byte, err error)
- func (o *SafeStringSliceMap) MarshalJSON() (data []byte, err error)
- func (o *SafeStringSliceMap) Merge(i StringMapI)
- func (o *SafeStringSliceMap) MergeMap(m map[string]string)
- func (o *SafeStringSliceMap) Range(f func(key string, value string) bool)
- func (o *SafeStringSliceMap) Set(key string, val string)
- func (o *SafeStringSliceMap) SetAt(index int, key string, val string)
- func (o *SafeStringSliceMap) SetChanged(key string, val string) (changed bool)
- func (o *SafeStringSliceMap) SetSortFunc(f func(key1, key2 string, val1, val2 string) bool) *SafeStringSliceMap
- func (o *SafeStringSliceMap) SortByKeys() *SafeStringSliceMap
- func (o *SafeStringSliceMap) SortByValues()
- func (o *SafeStringSliceMap) String() string
- func (o *SafeStringSliceMap) UnmarshalBinary(data []byte) (err error)
- func (o *SafeStringSliceMap) UnmarshalJSON(data []byte) (err error)
- func (o *SafeStringSliceMap) Values() (vals []string)
- type Setter
- type SliceMap
- func (o *SliceMap) Clear()
- func (o *SliceMap) Copy() *SliceMap
- func (o *SliceMap) Delete(key string)
- func (o *SliceMap) Equals(i MapI) bool
- func (o *SliceMap) Get(key string) (val interface{})
- func (o *SliceMap) GetAt(position int) (val interface{})
- func (o *SliceMap) GetKeyAt(position int) (key string)
- func (o *SliceMap) Has(key string) (ok bool)
- func (o *SliceMap) IsNil() bool
- func (o *SliceMap) Keys() (keys []string)
- func (o *SliceMap) Len() int
- func (o *SliceMap) Load(key string) (val interface{}, ok bool)
- func (o *SliceMap) LoadBool(key string) (val bool, ok bool)
- func (o *SliceMap) LoadFloat64(key string) (val float64, ok bool)
- func (o *SliceMap) LoadInt(key string) (val int, ok bool)
- func (o *SliceMap) LoadString(key string) (val string, ok bool)
- func (o *SliceMap) MarshalBinary() (data []byte, err error)
- func (o *SliceMap) MarshalJSON() (data []byte, err error)
- func (o *SliceMap) Merge(i MapI)
- func (o *SliceMap) MergeMap(m map[string]interface{})
- func (o *SliceMap) Range(f func(key string, value interface{}) bool)
- func (o *SliceMap) Set(key string, val interface{})
- func (o *SliceMap) SetAt(index int, key string, val interface{})
- func (o *SliceMap) SetSortFunc(f func(key1, key2 string, val1, val2 interface{}) bool) *SliceMap
- func (o *SliceMap) SortByKeys() *SliceMap
- func (o *SliceMap) String() string
- func (o *SliceMap) UnmarshalBinary(data []byte) (err error)
- func (o *SliceMap) UnmarshalJSON(data []byte) (err error)
- func (o *SliceMap) Values() (vals []interface{})
- type StringGetter
- type StringLoader
- type StringMap
- func (o *StringMap) Clear()
- func (o *StringMap) Copy() StringMapI
- func (o *StringMap) Delete(key string)
- func (o *StringMap) Equals(i StringMapI) bool
- func (o *StringMap) Get(key string) (val string)
- func (o *StringMap) Has(key string) (exists bool)
- func (o *StringMap) Is(key string, val string) (is bool)
- func (o *StringMap) IsNil() bool
- func (o *StringMap) Keys() (keys []string)
- func (o *StringMap) Len() (l int)
- func (o *StringMap) Load(key string) (val string, ok bool)
- func (o *StringMap) MarshalBinary() ([]byte, error)
- func (o *StringMap) MarshalJSON() (out []byte, err error)
- func (o *StringMap) Merge(i StringMapI)
- func (o *StringMap) MergeMap(m map[string]string)
- func (o *StringMap) Range(f func(key string, value string) bool)
- func (o *StringMap) Set(key string, val string)
- func (o *StringMap) SetChanged(key string, val string) (changed bool)
- func (o *StringMap) String() string
- func (o *StringMap) UnmarshalBinary(data []byte) (err error)
- func (o *StringMap) UnmarshalJSON(in []byte) (err error)
- func (o *StringMap) Values() (vals []string)
- type StringMapI
- type StringSetter
- type StringSliceMap
- func (o *StringSliceMap) Clear()
- func (o *StringSliceMap) Copy() *StringSliceMap
- func (o *StringSliceMap) Delete(key string)
- func (o *StringSliceMap) Equals(i StringMapI) bool
- func (o *StringSliceMap) Get(key string) (val string)
- func (o *StringSliceMap) GetAt(position int) (val string)
- func (o *StringSliceMap) GetKeyAt(position int) (key string)
- func (o *StringSliceMap) Has(key string) (ok bool)
- func (o *StringSliceMap) Is(key string, val string) (is bool)
- func (o *StringSliceMap) IsNil() bool
- func (o *StringSliceMap) Join(glue string) string
- func (o *StringSliceMap) Keys() (keys []string)
- func (o *StringSliceMap) Len() int
- func (o *StringSliceMap) Load(key string) (val string, ok bool)
- func (o *StringSliceMap) MarshalBinary() (data []byte, err error)
- func (o *StringSliceMap) MarshalJSON() (data []byte, err error)
- func (o *StringSliceMap) Merge(i StringMapI)
- func (o *StringSliceMap) MergeMap(m map[string]string)
- func (o *StringSliceMap) Range(f func(key string, value string) bool)
- func (o *StringSliceMap) Set(key string, val string)
- func (o *StringSliceMap) SetAt(index int, key string, val string)
- func (o *StringSliceMap) SetChanged(key string, val string) (changed bool)
- func (o *StringSliceMap) SetSortFunc(f func(key1, key2 string, val1, val2 string) bool) *StringSliceMap
- func (o *StringSliceMap) SortByKeys() *StringSliceMap
- func (o *StringSliceMap) SortByValues()
- func (o *StringSliceMap) String() string
- func (o *StringSliceMap) UnmarshalBinary(data []byte) (err error)
- func (o *StringSliceMap) UnmarshalJSON(data []byte) (err error)
- func (o *StringSliceMap) Values() (vals []string)
Examples ¶
- Map.Equals
- Map.Keys
- Map.MarshalBinary
- Map.MarshalJSON
- Map.Merge
- Map.MergeMap
- Map.Range
- Map.Set
- Map.UnmarshalJSON
- Map.Values
- NewMapFrom
- NewSafeMapFrom
- NewSafeSliceMapFrom
- NewSafeStringMapFrom
- NewSafeStringMapFromMap
- NewSafeStringSliceMapFrom
- NewSafeStringSliceMapFromMap
- NewSliceMapFrom
- NewStringMapFrom
- NewStringMapFromMap
- NewStringSliceMapFrom
- NewStringSliceMapFromMap
- SafeMap.Equals
- SafeMap.Keys
- SafeMap.MarshalBinary
- SafeMap.MarshalJSON
- SafeMap.Merge
- SafeMap.MergeMap
- SafeMap.Range
- SafeMap.Set
- SafeMap.UnmarshalJSON
- SafeMap.Values
- SafeSliceMap.Equals
- SafeSliceMap.Keys
- SafeSliceMap.MarshalBinary
- SafeSliceMap.MarshalJSON
- SafeSliceMap.Merge
- SafeSliceMap.MergeMap
- SafeSliceMap.Range
- SafeSliceMap.UnmarshalJSON
- SafeSliceMap.Values
- SafeStringMap.Equals
- SafeStringMap.Keys
- SafeStringMap.Merge
- SafeStringMap.MergeMap
- SafeStringMap.Range
- SafeStringMap.Set
- SafeStringMap.Values
- SafeStringSliceMap.Delete
- SafeStringSliceMap.Equals
- SafeStringSliceMap.Keys
- SafeStringSliceMap.MarshalJSON
- SafeStringSliceMap.Merge
- SafeStringSliceMap.MergeMap
- SafeStringSliceMap.Range
- SafeStringSliceMap.UnmarshalJSON
- SafeStringSliceMap.Values
- SliceMap.Equals
- SliceMap.Keys
- SliceMap.MarshalBinary
- SliceMap.MarshalJSON
- SliceMap.Merge
- SliceMap.MergeMap
- SliceMap.Range
- SliceMap.UnmarshalJSON
- SliceMap.Values
- StringMap.Equals
- StringMap.Keys
- StringMap.Merge
- StringMap.MergeMap
- StringMap.Range
- StringMap.Set
- StringMap.Values
- StringSliceMap.Delete
- StringSliceMap.Equals
- StringSliceMap.Keys
- StringSliceMap.MarshalJSON
- StringSliceMap.Merge
- StringSliceMap.MergeMap
- StringSliceMap.Range
- StringSliceMap.UnmarshalJSON
- StringSliceMap.Values
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map maps a string to a interface{}. This version is not safe for concurrent use. A zero value is ready for use, but you may not copy it after first using it.
func NewMapFrom ¶
NewMapFrom creates a new Map from a MapI interface object
Example ¶
n := NewMap() n.Set("a", "this") n.Set("b", 5) m := NewMapFrom(n) fmt.Println(m.Get("b"))
Output: 5
func NewMapFromMap ¶
NewMapFromMap creates a new Map from a GO map[string]interface{} object. Note that this will pass control of the given map to the new object. After you do this, DO NOT change the original map.
func (*Map) Delete ¶
Delete removes the key from the map. If the key does not exist, nothing happens.
func (*Map) Equals ¶
Equals returns true if all the keys in the given map exist in this map, and the values are the same
Example ¶
m := NewMap() m.Set("A", "This") m.Set("B", "That") n := NewMap() n.Set("B", "That") n.Set("A", "This") if m.Equals(n) { fmt.Print("Equal") } else { fmt.Print("Not Equal") }
Output: Equal
func (*Map) Get ¶
Get returns the value based on its key. If it does not exist, an empty string will be returned.
func (*Map) Keys ¶
Keys returns a slice of the keys. It will return a nil slice if the map is empty. Multiple calls to Keys will result in the same list of keys, but may be in a different order.
Example ¶
m := NewMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Keys() sort.Sort(sort.StringSlice(values)) fmt.Println(values)
Output: [A B C]
func (*Map) Load ¶
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load()
func (*Map) MarshalBinary ¶
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.
Example ¶
// You would rarely call MarshallBinary directly, but rather would use an encoder, like GOB for binary encoding m := new(Map) var m2 Map m.Set("B", "This") m.Set("A", "That") m.Set("C", 3) var buf bytes.Buffer enc := gob.NewEncoder(&buf) // Will write dec := gob.NewDecoder(&buf) // Will read enc.Encode(m) dec.Decode(&m2) s := m2.Get("A") fmt.Println(s) s = m2.Get("C") fmt.Println(s)
Output: That 3
func (*Map) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
Example ¶
// You don't normally call MarshallJSON directly, but rather use the Marshall and Unmarshall json commands m := new(Map) m.Set("B", "This") m.Set("A", "That") m.Set("C", 3) s, _ := json.Marshal(m) // Note: The below output is what is produced, but isn't guaranteed. go seems to currently be sorting keys os.Stdout.Write(s)
Output: {"A":"That","B":"This","C":3}
func (*Map) Merge ¶
Merge merges the given map with the current one. The given one takes precedent on collisions.
Example ¶
m := NewMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") n := NewMap() n.Set("D", 5) n.Merge(m) fmt.Println(n.Get("C")) fmt.Println(n.Get("D"))
Output: Other 5
func (*Map) MergeMap ¶ added in v0.3.0
MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
Example ¶
m := map[string]interface{}{ "B": "This", "A": "That", "C": 6.1, } n := NewMap() n.Set("D", "Last") n.MergeMap(m) fmt.Println(n.Get("C")) fmt.Println(n.Get("D"))
Output: 6.1 Last
func (*Map) Range ¶
Range will call the given function with every key and value in the map. If f returns false, it stops the iteration. This pattern is taken from sync.Map.
Example ¶
m := NewMap() a := []string{} m.Set("B", "This") m.Set("A", "That") m.Set("C", 5) m.Range(func(key string, val interface{}) bool { a = append(a, fmt.Sprintf("%v", val)) return true // keep iterating to the end }) fmt.Println() sort.Sort(sort.StringSlice(a)) // Unordered maps cannot be guaranteed to range in a particular order. Sort it so we can compare it. fmt.Println(a)
Output: [5 That This]
func (*Map) Set ¶
Set sets the key to the given value
Example ¶
m := NewMap() m.Set("a", "Here") fmt.Println(m.String())
Output: {"a":"Here"}
func (*Map) UnmarshalBinary ¶
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a Map
func (*Map) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a Map. The JSON must start with an object.
Example ¶
b := []byte(`{"A":"That","B":"This","C":3}`) var m Map json.Unmarshal(b, &m) fmt.Println(m.Get("C"))
Output: 3
func (*Map) Values ¶
func (o *Map) Values() (vals []interface{})
Values returns a slice of the values. It will return a nil slice if the map is empty. Multiple calls to Values will result in the same list of values, but may be in a different order.
Example ¶
m := NewMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", 5) values := m.Values() var values2 []string for _, value := range values { values2 = append(values2, fmt.Sprintf("%v", value)) } sort.Sort(sort.StringSlice(values2)) fmt.Println(values2)
Output: [5 That This]
type MapI ¶
type MapI interface { Get(key string) (val interface{}) Has(key string) (exists bool) Values() []interface{} Keys() []string Len() int // Range will iterate over the keys and values in the map. Pattern is taken from sync.Map Range(f func(key string, value interface{}) bool) Merge(i MapI) String() string }
The MapI interface provides a common interface to the many kinds of similar map objects.
Most functions that change the map are omitted so that you can wrap the map in additional functionality that might use Set or SetChanged. If you want to use them in an interface setting, you can create your own interface that includes them.
type SafeMap ¶
SafeMap maps a string to a interface{}. This version is safe for concurrent use. A zero value is ready for use, but you may not copy it after first using it.
func NewSafeMap ¶
func NewSafeMap() *SafeMap
NewSafeMap creates a new map that maps string's to interface{}'s.
func NewSafeMapFrom ¶
NewSafeMapFrom creates a new SafeMap from a MapI interface object
Example ¶
n := NewSafeMap() n.Set("a", "this") n.Set("b", 5) m := NewSafeMapFrom(n) fmt.Println(m.Get("b"))
Output: 5
func NewSafeMapFromMap ¶
NewSafeMapFromMap creates a new SafeMap from a GO map[string]interface{} object. Note that this will pass control of the given map to the new object. After you do this, DO NOT change the original map.
func (*SafeMap) Delete ¶
Delete removes the key from the map. If the key does not exist, nothing happens.
func (*SafeMap) Equals ¶
Equals returns true if all the keys in the given map exist in this map, and the values are the same
Example ¶
m := NewSafeMap() m.Set("A", "This") m.Set("B", "That") n := NewSafeMap() n.Set("B", "That") n.Set("A", "This") if m.Equals(n) { fmt.Print("Equal") } else { fmt.Print("Not Equal") }
Output: Equal
func (*SafeMap) Get ¶
Get returns the value based on its key. If it does not exist, an empty string will be returned.
func (*SafeMap) Keys ¶
Keys returns a slice of the keys. It will return a nil slice if the map is empty. Multiple calls to Keys will result in the same list of keys, but may be in a different order.
Example ¶
m := NewSafeMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Keys() sort.Sort(sort.StringSlice(values)) fmt.Println(values)
Output: [A B C]
func (*SafeMap) Load ¶
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load()
func (*SafeMap) MarshalBinary ¶
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.
Example ¶
// You would rarely call MarshallBinary directly, but rather would use an encoder, like GOB for binary encoding m := new(SafeMap) var m2 SafeMap m.Set("B", "This") m.Set("A", "That") m.Set("C", 3) var buf bytes.Buffer enc := gob.NewEncoder(&buf) // Will write dec := gob.NewDecoder(&buf) // Will read enc.Encode(m) dec.Decode(&m2) s := m2.Get("A") fmt.Println(s) s = m2.Get("C") fmt.Println(s)
Output: That 3
func (*SafeMap) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
Example ¶
// You don't normally call MarshallJSON directly, but rather use the Marshall and Unmarshall json commands m := new(SafeMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", 3) s, _ := json.Marshal(m) // Note: The below output is what is produced, but isn't guaranteed. go seems to currently be sorting keys os.Stdout.Write(s)
Output: {"A":"That","B":"This","C":3}
func (*SafeMap) Merge ¶
Merge merges the given map with the current one. The given one takes precedent on collisions.
Example ¶
m := NewSafeMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") n := NewSafeMap() n.Set("D", 5) n.Merge(m) fmt.Println(n.Get("C")) fmt.Println(n.Get("D"))
Output: Other 5
func (*SafeMap) MergeMap ¶ added in v0.3.0
MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
Example ¶
m := map[string]interface{}{ "B": "This", "A": "That", "C": 6.1, } n := NewSafeMap() n.Set("D", "Last") n.MergeMap(m) fmt.Println(n.Get("C")) fmt.Println(n.Get("D"))
Output: 6.1 Last
func (*SafeMap) Range ¶
Range will call the given function with every key and value in the map. If f returns false, it stops the iteration. This pattern is taken from sync.Map. During this process, the map will be locked, so do not pass a function that will take significant amounts of time.
Example ¶
m := NewSafeMap() a := []string{} m.Set("B", "This") m.Set("A", "That") m.Set("C", 5) m.Range(func(key string, val interface{}) bool { a = append(a, fmt.Sprintf("%v", val)) return true // keep iterating to the end }) fmt.Println() sort.Sort(sort.StringSlice(a)) // Unordered maps cannot be guaranteed to range in a particular order. Sort it so we can compare it. fmt.Println(a)
Output: [5 That This]
func (*SafeMap) Set ¶
Set sets the key to the given value
Example ¶
m := NewSafeMap() m.Set("a", "Here") fmt.Println(m.String())
Output: {"a":"Here"}
func (*SafeMap) UnmarshalBinary ¶
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SafeMap
func (*SafeMap) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a SafeMap. The JSON must start with an object.
Example ¶
b := []byte(`{"A":"That","B":"This","C":3}`) var m SafeMap json.Unmarshal(b, &m) fmt.Println(m.Get("C"))
Output: 3
func (*SafeMap) Values ¶
func (o *SafeMap) Values() (vals []interface{})
Values returns a slice of the values. It will return a nil slice if the map is empty. Multiple calls to Values will result in the same list of values, but may be in a different order.
Example ¶
m := NewSafeMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", 5) values := m.Values() var values2 []string for _, value := range values { values2 = append(values2, fmt.Sprintf("%v", value)) } sort.Sort(sort.StringSlice(values2)) fmt.Println(values2)
Output: [5 That This]
type SafeSliceMap ¶
A SafeSliceMap combines a map with a slice so that you can range over a map in a predictable order. By default, the order will be the same order that items were inserted, i.e. a FIFO list. This is similar to how PHP arrays work. SafeSliceMap implements the sort interface so you can change the order before ranging over the values if desired. It is safe for concurrent use. The zero of this is usable immediately. The SafeSliceMap satisfies the MapI interface.
func NewSafeSliceMap ¶
func NewSafeSliceMap() *SafeSliceMap
NewSafeSliceMap creates a new map that maps string's to interface{}'s.
func NewSafeSliceMapFrom ¶
func NewSafeSliceMapFrom(i MapI) *SafeSliceMap
NewSafeSliceMapFrom creates a new SafeMap from a MapI interface object
Example ¶
n := new(Map) n.Set("a", "this") n.Set("b", "that") m := NewSafeSliceMapFrom(n) fmt.Println(m.Get("b"))
Output: that
func NewSafeSliceMapFromMap ¶
func NewSafeSliceMapFromMap(i map[string]interface{}) *SafeSliceMap
NewSafeSliceMapFromMap creates a new SafeSliceMap from a GO map[string]interface{} object. Note that this will pass control of the given map to the new object. After you do this, DO NOT change the original map.
func (*SafeSliceMap) Clear ¶
func (o *SafeSliceMap) Clear()
func (*SafeSliceMap) Copy ¶
func (o *SafeSliceMap) Copy() *SafeSliceMap
Copy will make a copy of the map and a copy of the underlying data.
func (*SafeSliceMap) Delete ¶
func (o *SafeSliceMap) Delete(key string)
Delete removes the item with the given key.
func (*SafeSliceMap) Equals ¶
func (o *SafeSliceMap) Equals(i MapI) bool
Equals returns true if the map equals the given map, paying attention only to the content of the map and not the order.
Example ¶
n := new(Map) n.Set("A", "This") n.Set("B", "That") m := NewSafeSliceMapFrom(n) if m.Equals(n) { fmt.Print("Equal") } else { fmt.Print("Not Equal") }
Output: Equal
func (*SafeSliceMap) Get ¶
func (o *SafeSliceMap) Get(key string) (val interface{})
Get returns the value based on its key. If the key does not exist, an empty value is returned.
func (*SafeSliceMap) GetAt ¶
func (o *SafeSliceMap) GetAt(position int) (val interface{})
GetAt returns the value based on its position. If the position is out of bounds, an empty value is returned.
func (*SafeSliceMap) GetKeyAt ¶ added in v0.1.5
func (o *SafeSliceMap) GetKeyAt(position int) (key string)
GetKeyAt returns the key based on its position. If the position is out of bounds, an empty value is returned.
func (*SafeSliceMap) Has ¶
func (o *SafeSliceMap) Has(key string) (ok bool)
Has returns true if the given key exists in the map.
func (*SafeSliceMap) IsNil ¶
func (o *SafeSliceMap) IsNil() bool
func (*SafeSliceMap) Keys ¶
func (o *SafeSliceMap) Keys() (keys []string)
Keys returns the keys of the map, in the order they were added or sorted
Example ¶
m := new(SafeSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Keys() fmt.Println(values)
Output: [B A C]
func (*SafeSliceMap) Len ¶
func (o *SafeSliceMap) Len() int
Len returns the number of items in the map
func (*SafeSliceMap) Load ¶
func (o *SafeSliceMap) Load(key string) (val interface{}, ok bool)
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load()
func (*SafeSliceMap) LoadFloat64 ¶
func (o *SafeSliceMap) LoadFloat64(key string) (val float64, ok bool)
func (*SafeSliceMap) LoadString ¶
func (o *SafeSliceMap) LoadString(key string) (val string, ok bool)
func (*SafeSliceMap) MarshalBinary ¶
func (o *SafeSliceMap) MarshalBinary() (data []byte, err error)
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream. If you are using a sort function, you must save and restore the sort function in a separate operation since functions are not serializable.
Example ¶
// You would rarely call MarshallBinary directly, but rather would use an encoder, like GOB for binary encoding m := new(SafeSliceMap) var m2 SafeSliceMap m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") var buf bytes.Buffer enc := gob.NewEncoder(&buf) // Will write dec := gob.NewDecoder(&buf) // Will read enc.Encode(m) dec.Decode(&m2) s := m2.Get("A") fmt.Println(s) s = m2.GetAt(2) fmt.Println(s)
Output: That Other
func (*SafeSliceMap) MarshalJSON ¶
func (o *SafeSliceMap) MarshalJSON() (data []byte, err error)
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
Example ¶
// You don't normally call MarshallJSON directly, but rather use the Marshall and Unmarshall json commands m := new(SafeSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") s, _ := json.Marshal(m) // Note: The below output is what is produced, but isn't guaranteed. go seems to currently be sorting keys os.Stdout.Write(s)
Output: {"A":"That","B":"This","C":"Other"}
func (*SafeSliceMap) Merge ¶
func (o *SafeSliceMap) Merge(i MapI)
Merge the given map into the current one
Example ¶
m := new(SafeSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", 5) n := new(SafeSliceMap) n.SortByKeys() n.Set("D", "Last") n.Merge(m) values := n.Values() fmt.Println(values)
Output: [That This 5 Last]
func (*SafeSliceMap) MergeMap ¶ added in v0.3.0
func (o *SafeSliceMap) MergeMap(m map[string]interface{})
MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
Example ¶
m := map[string]interface{}{ "B": "This", "A": "That", "C": 5, } n := NewSafeSliceMap() n.SortByKeys() n.Set("D", "Last") n.MergeMap(m) values := n.Values() fmt.Println(values)
Output: [That This 5 Last]
func (*SafeSliceMap) Range ¶
func (o *SafeSliceMap) Range(f func(key string, value interface{}) bool)
Range will call the given function with every key and value in the order they were placed in the map, or in if you sorted the map, in your custom order. If f returns false, it stops the iteration. This pattern is taken from sync.Map.
Example ¶
m := new(SafeSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") // Iterate by insertion order m.Range(func(key string, val interface{}) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println() // Iterate after sorting keys m.SortByKeys() m.Set("D", "Other2") m.Range(func(key string, val interface{}) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println()
Output: B:This,A:That,C:Other, A:That,B:This,C:Other,D:Other2,
func (*SafeSliceMap) Set ¶
func (o *SafeSliceMap) Set(key string, val interface{})
Set sets the given key to the given value. If the key already exists, the range order will not change.
func (*SafeSliceMap) SetAt ¶
func (o *SafeSliceMap) SetAt(index int, key string, val interface{})
SetAt sets the given key to the given value, but also inserts it at the index specified. If the index is bigger than the length, it puts it at the end. Negative indexes are backwards from the end.
func (*SafeSliceMap) SetSortFunc ¶ added in v0.2.0
func (o *SafeSliceMap) SetSortFunc(f func(key1, key2 string, val1, val2 interface{}) bool) *SafeSliceMap
SetSortFunc sets the sort function which will determine the order of the items in the map on an ongoing basis. Normally, items will iterate in the order they were added. The sort function is a Less function, that returns true when item 1 is "less" than item 2. The sort function receives both the keys and values, so it can use either to decide how to sort.
func (*SafeSliceMap) SortByKeys ¶ added in v0.2.0
func (o *SafeSliceMap) SortByKeys() *SafeSliceMap
SortByKeys sets up the map to have its sort order sort by keys, lowest to highest
func (*SafeSliceMap) String ¶
func (o *SafeSliceMap) String() string
func (*SafeSliceMap) UnmarshalBinary ¶
func (o *SafeSliceMap) UnmarshalBinary(data []byte) (err error)
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SafeSliceMap
func (*SafeSliceMap) UnmarshalJSON ¶
func (o *SafeSliceMap) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a SafeMap. The JSON must start with an object.
Example ¶
b := []byte(`{"A":"That","B":"This","C":"Other"}`) var m SafeSliceMap json.Unmarshal(b, &m) m.SortByKeys() fmt.Println(&m)
Output: {"A":"That","B":"This","C":"Other"}
func (*SafeSliceMap) Values ¶
func (o *SafeSliceMap) Values() (vals []interface{})
Values returns a slice of the values in the order they were added or sorted.
Example ¶
m := new(SafeSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Values() fmt.Println(values)
Output: [This That Other]
type SafeStringMap ¶
SafeStringMap maps a string to a string. This version is safe for concurrent use. A zero value is ready for use, but you may not copy it after first using it.
func NewSafeStringMap ¶
func NewSafeStringMap() *SafeStringMap
NewSafeStringMap creates a new map that maps string's to string's.
func NewSafeStringMapFrom ¶
func NewSafeStringMapFrom(i StringMapI) *SafeStringMap
NewSafeStringMapFrom creates a new SafeStringMap from a StringMapI interface object
Example ¶
n := NewSafeStringMap() n.Set("a", "this") n.Set("b", "that") m := NewSafeStringMapFrom(n) fmt.Println(m.Get("b"))
Output: that
func NewSafeStringMapFromMap ¶
func NewSafeStringMapFromMap(i map[string]string) *SafeStringMap
NewSafeStringMapFromMap creates a new SafeStringMap from a GO map[string]string object. Note that this will pass control of the given map to the new object. After you do this, DO NOT change the original map.
Example ¶
n := map[string]string{"a": "this", "b": "that"} m := NewSafeStringMapFromMap(n) fmt.Println(m.String())
Output: {"a":"this","b":"that"}
func (*SafeStringMap) Copy ¶
func (o *SafeStringMap) Copy() StringMapI
Copy will make a copy of the map and a copy of the underlying data.
func (*SafeStringMap) Delete ¶
func (o *SafeStringMap) Delete(key string)
Delete removes the key from the map. If the key does not exist, nothing happens.
func (*SafeStringMap) Equals ¶
func (o *SafeStringMap) Equals(i StringMapI) bool
Equals returns true if all the keys in the given map exist in this map, and the values are the same
Example ¶
m := NewSafeStringMap() m.Set("A", "This") m.Set("B", "That") n := NewSafeStringMap() n.Set("B", "That") n.Set("A", "This") if m.Equals(n) { fmt.Print("Equal") } else { fmt.Print("Not Equal") }
Output: Equal
func (*SafeStringMap) Get ¶
func (o *SafeStringMap) Get(key string) (val string)
Get returns the value based on its key. If it does not exist, an empty string will be returned.
func (*SafeStringMap) Has ¶
func (o *SafeStringMap) Has(key string) (exists bool)
Has returns true if the given key exists in the map.
func (*SafeStringMap) Is ¶ added in v0.3.0
func (o *SafeStringMap) Is(key string, val string) (is bool)
Is returns true if the given key exists in the map and has the given value.
func (*SafeStringMap) IsNil ¶
func (o *SafeStringMap) IsNil() bool
func (*SafeStringMap) Keys ¶
func (o *SafeStringMap) Keys() (keys []string)
Keys returns a slice of the keys. It will return a nil slice if the map is empty. Multiple calls to Keys will result in the same list of keys, but may be in a different order.
Example ¶
m := NewSafeStringMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Keys() sort.Sort(sort.StringSlice(values)) fmt.Println(values)
Output: [A B C]
func (*SafeStringMap) Len ¶
func (o *SafeStringMap) Len() (l int)
Len returns the number of items in the map
func (*SafeStringMap) Load ¶
func (o *SafeStringMap) Load(key string) (val string, ok bool)
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load()
func (*SafeStringMap) MarshalBinary ¶
func (o *SafeStringMap) MarshalBinary() ([]byte, error)
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.
func (*SafeStringMap) MarshalJSON ¶
func (o *SafeStringMap) MarshalJSON() (out []byte, err error)
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (*SafeStringMap) Merge ¶
func (o *SafeStringMap) Merge(i StringMapI)
Merge merges the given map with the current one. The given one takes precedent on collisions.
Example ¶
m := NewSafeStringMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") n := NewSafeStringMap() n.Set("D", "Last") n.Merge(m) fmt.Println(n.Get("C")) fmt.Println(n.Get("D"))
Output: Other Last
func (*SafeStringMap) MergeMap ¶ added in v0.3.0
func (o *SafeStringMap) MergeMap(m map[string]string)
MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
Example ¶
m := map[string]string{ "B": "This", "A": "That", "C": "Other", } n := NewSafeStringMap() n.Set("D", "Last") n.MergeMap(m) fmt.Println(n.Get("C")) fmt.Println(n.Get("D"))
Output: Other Last
func (*SafeStringMap) Range ¶
func (o *SafeStringMap) Range(f func(key string, value string) bool)
Range will call the given function with every key and value in the map. If f returns false, it stops the iteration. This pattern is taken from sync.Map. During this process, the map will be locked, so do not pass a function that will take significant amounts of time.
Example ¶
m := NewSafeStringMap() var a []string m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") m.Range(func(key string, val string) bool { a = append(a, val) return true // keep iterating to the end }) fmt.Println() sort.Sort(sort.StringSlice(a)) // unordered maps cannot be guaranteed to range in a particular order. Sort it so we can compare it. fmt.Println(a)
Output: [Other That This]
func (*SafeStringMap) Set ¶
func (o *SafeStringMap) Set(key string, val string)
Set sets the key to the given value
Example ¶
m := NewSafeStringMap() m.Set("a", "Here") fmt.Println(m.Get("a")) // Output Here
Output:
func (*SafeStringMap) SetChanged ¶
func (o *SafeStringMap) SetChanged(key string, val string) (changed bool)
SetChanged sets the key to the value and returns a boolean indicating whether doing this caused the map to change. It will return true if the key did not first exist, or if the value associated with the key was different than the new value.
func (*SafeStringMap) String ¶ added in v0.2.0
func (o *SafeStringMap) String() string
func (*SafeStringMap) UnmarshalBinary ¶
func (o *SafeStringMap) UnmarshalBinary(data []byte) (err error)
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SafeStringMap
func (*SafeStringMap) UnmarshalJSON ¶
func (o *SafeStringMap) UnmarshalJSON(in []byte) (err error)
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a SafeStringMap. The JSON must start with an object.
func (*SafeStringMap) Values ¶
func (o *SafeStringMap) Values() (vals []string)
Values returns a slice of the values. It will return a nil slice if the map is empty. Multiple calls to Values will result in the same list of values, but may be in a different order.
Example ¶
m := NewSafeStringMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Values() sort.Sort(sort.StringSlice(values)) fmt.Println(values)
Output: [Other That This]
type SafeStringSliceMap ¶
A SafeStringSliceMap combines a map with a slice so that you can range over a map in a predictable order. By default, the order will be the same order that items were inserted, i.e. a FIFO list. This is similar to how PHP arrays work. SafeStringSliceMap implements the sort interface so you can change the order before ranging over the values if desired. It is safe for concurrent use. The zero of this is usable immediately. The SafeStringSliceMap satisfies the StringMapI interface.
func NewSafeStringSliceMap ¶
func NewSafeStringSliceMap() *SafeStringSliceMap
NewSafeStringSliceMap creates a new map that maps string's to string's.
func NewSafeStringSliceMapFrom ¶
func NewSafeStringSliceMapFrom(i StringMapI) *SafeStringSliceMap
NewSafeStringSliceMapFrom creates a new SafeStringMap from a StringMapI interface object
Example ¶
n := new(StringMap) n.Set("a", "this") n.Set("b", "that") m := NewSafeStringSliceMapFrom(n) fmt.Println(m.Get("b"))
Output: that
func NewSafeStringSliceMapFromMap ¶
func NewSafeStringSliceMapFromMap(i map[string]string) *SafeStringSliceMap
NewSafeStringSliceMapFromMap creates a new SafeStringSliceMap from a GO map[string]string object. Note that this will pass control of the given map to the new object. After you do this, DO NOT change the original map.
Example ¶
n := map[string]string{"a": "this", "b": "that"} m := NewSafeStringSliceMapFromMap(n) m.SortByKeys() fmt.Println(m.String())
Output: {"a":"this","b":"that"}
func (*SafeStringSliceMap) Clear ¶
func (o *SafeStringSliceMap) Clear()
func (*SafeStringSliceMap) Copy ¶
func (o *SafeStringSliceMap) Copy() *SafeStringSliceMap
Copy will make a copy of the map and a copy of the underlying data.
func (*SafeStringSliceMap) Delete ¶
func (o *SafeStringSliceMap) Delete(key string)
Delete removes the item with the given key.
Example ¶
n := map[string]string{"a": "this", "b": "that", "c": "other"} m := NewSafeStringSliceMapFromMap(n) m.SortByKeys() m.Delete("b") fmt.Println(m.String())
Output: {"a":"this","c":"other"}
func (*SafeStringSliceMap) Equals ¶
func (o *SafeStringSliceMap) Equals(i StringMapI) bool
Equals returns true if the map equals the given map, paying attention only to the content of the map and not the order.
Example ¶
n := new(StringMap) n.Set("A", "This") n.Set("B", "That") m := NewSafeStringSliceMapFrom(n) if m.Equals(n) { fmt.Println("Equal") } else { fmt.Println("Not Equal") } m.Set("B", "Other") if m.Equals(n) { fmt.Println("Equal") } else { fmt.Println("Not Equal") }
Output: Equal Not Equal
func (*SafeStringSliceMap) Get ¶
func (o *SafeStringSliceMap) Get(key string) (val string)
Get returns the value based on its key. If the key does not exist, an empty value is returned.
func (*SafeStringSliceMap) GetAt ¶
func (o *SafeStringSliceMap) GetAt(position int) (val string)
GetAt returns the value based on its position. If the position is out of bounds, an empty value is returned.
func (*SafeStringSliceMap) GetKeyAt ¶ added in v0.1.5
func (o *SafeStringSliceMap) GetKeyAt(position int) (key string)
GetKeyAt returns the key based on its position. If the position is out of bounds, an empty value is returned.
func (*SafeStringSliceMap) Has ¶
func (o *SafeStringSliceMap) Has(key string) (ok bool)
Has returns true if the given key exists in the map.
func (*SafeStringSliceMap) Is ¶ added in v0.3.0
func (o *SafeStringSliceMap) Is(key string, val string) (is bool)
Is returns true if the given key exists in the map and has the given value.
func (*SafeStringSliceMap) IsNil ¶
func (o *SafeStringSliceMap) IsNil() bool
func (*SafeStringSliceMap) Join ¶
func (o *SafeStringSliceMap) Join(glue string) string
Join is just like strings.Join
func (*SafeStringSliceMap) Keys ¶
func (o *SafeStringSliceMap) Keys() (keys []string)
Keys returns the keys of the map, in the order they were added or sorted
Example ¶
m := new(SafeStringSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Keys() fmt.Println(values)
Output: [B A C]
func (*SafeStringSliceMap) Len ¶
func (o *SafeStringSliceMap) Len() int
Len returns the number of items in the map
func (*SafeStringSliceMap) Load ¶
func (o *SafeStringSliceMap) Load(key string) (val string, ok bool)
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load()
func (*SafeStringSliceMap) MarshalBinary ¶
func (o *SafeStringSliceMap) MarshalBinary() (data []byte, err error)
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream. If you are using a sort function, you must save and restore the sort function in a separate operation since functions are not serializable.
func (*SafeStringSliceMap) MarshalJSON ¶
func (o *SafeStringSliceMap) MarshalJSON() (data []byte, err error)
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
Example ¶
// You don't normally call MarshallJSON directly, but rather use the Marshall and Unmarshall json commands m := new(SafeStringSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") s, _ := json.Marshal(m) os.Stdout.Write(s) // Note: The below output is what is produced, but isn't guaranteed. go seems to currently be sorting keys
Output:
func (*SafeStringSliceMap) Merge ¶
func (o *SafeStringSliceMap) Merge(i StringMapI)
Merge the given map into the current one
Example ¶
m := new(SafeStringSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") n := new(SafeStringSliceMap) n.SortByKeys() n.Set("D", "Last") n.Merge(m) values := n.Values() fmt.Println(values)
Output: [That This Other Last]
func (*SafeStringSliceMap) MergeMap ¶ added in v0.3.0
func (o *SafeStringSliceMap) MergeMap(m map[string]string)
MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
Example ¶
m := map[string]string{ "B": "This", "A": "That", "C": "Other", } n := NewSafeStringSliceMap() n.SortByKeys() n.Set("D", "Last") n.MergeMap(m) values := n.Values() fmt.Println(values)
Output: [That This Other Last]
func (*SafeStringSliceMap) Range ¶
func (o *SafeStringSliceMap) Range(f func(key string, value string) bool)
Range will call the given function with every key and value in the order they were placed in the map, or in if you sorted the map, in your custom order. If f returns false, it stops the iteration. This pattern is taken from sync.Map.
Example ¶
m := new(SafeStringSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") // Iterate by insertion order m.Range(func(key string, val string) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println() m.SortByValues() m.Set("D", "Other2") // test adding value after sorting m.Range(func(key string, val string) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println() m.SortByKeys() m.Range(func(key string, val string) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println()
Output: B:This,A:That,C:Other, C:Other,D:Other2,A:That,B:This, A:That,B:This,C:Other,D:Other2,
func (*SafeStringSliceMap) Set ¶
func (o *SafeStringSliceMap) Set(key string, val string)
Set sets the given key to the given value. If the key already exists, the range order will not change.
func (*SafeStringSliceMap) SetAt ¶
func (o *SafeStringSliceMap) SetAt(index int, key string, val string)
SetAt sets the given key to the given value, but also inserts it at the index specified. If the index is bigger than the length, it puts it at the end. Negative indexes are backwards from the end.
func (*SafeStringSliceMap) SetChanged ¶
func (o *SafeStringSliceMap) SetChanged(key string, val string) (changed bool)
SetChanged sets the value. It returns true if something in the map changed. If the key was already in the map, and you have not provided a sort function, the order will not change, but the value will be replaced. If you wanted the order to change, you must Delete then call SetChanged. If you have previously set a sort function, the order will be updated.
func (*SafeStringSliceMap) SetSortFunc ¶ added in v0.2.0
func (o *SafeStringSliceMap) SetSortFunc(f func(key1, key2 string, val1, val2 string) bool) *SafeStringSliceMap
SetSortFunc sets the sort function which will determine the order of the items in the map on an ongoing basis. Normally, items will iterate in the order they were added. The sort function is a Less function, that returns true when item 1 is "less" than item 2. The sort function receives both the keys and values, so it can use either to decide how to sort.
func (*SafeStringSliceMap) SortByKeys ¶ added in v0.2.0
func (o *SafeStringSliceMap) SortByKeys() *SafeStringSliceMap
SortByKeys sets up the map to have its sort order sort by keys, lowest to highest
func (*SafeStringSliceMap) SortByValues ¶ added in v0.2.0
func (o *SafeStringSliceMap) SortByValues()
SortByValues sets up the map to have its sort order sort by values, lowest to highest
func (*SafeStringSliceMap) String ¶
func (o *SafeStringSliceMap) String() string
func (*SafeStringSliceMap) UnmarshalBinary ¶
func (o *SafeStringSliceMap) UnmarshalBinary(data []byte) (err error)
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SafeStringSliceMap
func (*SafeStringSliceMap) UnmarshalJSON ¶
func (o *SafeStringSliceMap) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a SafeStringMap. The JSON must start with an object.
Example ¶
b := []byte(`{"A":"That","B":"This","C":"Other"}`) var m SafeStringSliceMap json.Unmarshal(b, &m) m.SortByKeys() fmt.Println(&m)
Output: {"A":"That","B":"This","C":"Other"}
func (*SafeStringSliceMap) Values ¶
func (o *SafeStringSliceMap) Values() (vals []string)
Values returns a slice of the values in the order they were added or sorted.
Example ¶
m := new(SafeStringSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Values() fmt.Println(values)
Output: [This That Other]
type SliceMap ¶
type SliceMap struct {
// contains filtered or unexported fields
}
A SliceMap combines a map with a slice so that you can range over a map in a predictable order. By default, the order will be the same order that items were inserted, i.e. a FIFO list. This is similar to how PHP arrays work. SliceMap implements the sort interface so you can change the order before ranging over the values if desired. It is NOT safe for concurrent use. The zero of this is usable immediately. The SliceMap satisfies the MapI interface.
func NewSliceMap ¶
func NewSliceMap() *SliceMap
NewSliceMap creates a new map that maps string's to interface{}'s.
func NewSliceMapFrom ¶
NewSliceMapFrom creates a new Map from a MapI interface object
Example ¶
n := new(Map) n.Set("a", "this") n.Set("b", "that") m := NewSliceMapFrom(n) fmt.Println(m.Get("b"))
Output: that
func NewSliceMapFromMap ¶
NewSliceMapFromMap creates a new SliceMap from a GO map[string]interface{} object. Note that this will pass control of the given map to the new object. After you do this, DO NOT change the original map.
func (*SliceMap) Equals ¶
Equals returns true if the map equals the given map, paying attention only to the content of the map and not the order.
Example ¶
n := new(Map) n.Set("A", "This") n.Set("B", "That") m := NewSliceMapFrom(n) if m.Equals(n) { fmt.Print("Equal") } else { fmt.Print("Not Equal") }
Output: Equal
func (*SliceMap) Get ¶
Get returns the value based on its key. If the key does not exist, an empty value is returned.
func (*SliceMap) GetAt ¶
GetAt returns the value based on its position. If the position is out of bounds, an empty value is returned.
func (*SliceMap) GetKeyAt ¶ added in v0.1.5
GetKeyAt returns the key based on its position. If the position is out of bounds, an empty value is returned.
func (*SliceMap) Keys ¶
Keys returns the keys of the map, in the order they were added or sorted
Example ¶
m := new(SliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Keys() fmt.Println(values)
Output: [B A C]
func (*SliceMap) Load ¶
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load()
func (*SliceMap) MarshalBinary ¶
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream. If you are using a sort function, you must save and restore the sort function in a separate operation since functions are not serializable.
Example ¶
// You would rarely call MarshallBinary directly, but rather would use an encoder, like GOB for binary encoding m := new(SliceMap) var m2 SliceMap m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") var buf bytes.Buffer enc := gob.NewEncoder(&buf) // Will write dec := gob.NewDecoder(&buf) // Will read enc.Encode(m) dec.Decode(&m2) s := m2.Get("A") fmt.Println(s) s = m2.GetAt(2) fmt.Println(s)
Output: That Other
func (*SliceMap) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
Example ¶
// You don't normally call MarshallJSON directly, but rather use the Marshall and Unmarshall json commands m := new(SliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") s, _ := json.Marshal(m) // Note: The below output is what is produced, but isn't guaranteed. go seems to currently be sorting keys os.Stdout.Write(s)
Output: {"A":"That","B":"This","C":"Other"}
func (*SliceMap) Merge ¶
Merge the given map into the current one
Example ¶
m := new(SliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", 5) n := new(SliceMap) n.SortByKeys() n.Set("D", "Last") n.Merge(m) values := n.Values() fmt.Println(values)
Output: [That This 5 Last]
func (*SliceMap) MergeMap ¶ added in v0.3.0
MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
Example ¶
m := map[string]interface{}{ "B": "This", "A": "That", "C": 5, } n := NewSliceMap() n.SortByKeys() n.Set("D", "Last") n.MergeMap(m) values := n.Values() fmt.Println(values)
Output: [That This 5 Last]
func (*SliceMap) Range ¶
Range will call the given function with every key and value in the order they were placed in the map, or in if you sorted the map, in your custom order. If f returns false, it stops the iteration. This pattern is taken from sync.Map.
Example ¶
m := new(SliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") // Iterate by insertion order m.Range(func(key string, val interface{}) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println() // Iterate after sorting keys m.SortByKeys() m.Set("D", "Other2") m.Range(func(key string, val interface{}) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println()
Output: B:This,A:That,C:Other, A:That,B:This,C:Other,D:Other2,
func (*SliceMap) Set ¶
Set sets the given key to the given value. If the key already exists, the range order will not change.
func (*SliceMap) SetAt ¶
SetAt sets the given key to the given value, but also inserts it at the index specified. If the index is bigger than the length, it puts it at the end. Negative indexes are backwards from the end.
func (*SliceMap) SetSortFunc ¶ added in v0.2.0
SetSortFunc sets the sort function which will determine the order of the items in the map on an ongoing basis. Normally, items will iterate in the order they were added. The sort function is a Less function, that returns true when item 1 is "less" than item 2. The sort function receives both the keys and values, so it can use either to decide how to sort.
func (*SliceMap) SortByKeys ¶ added in v0.2.0
SortByKeys sets up the map to have its sort order sort by keys, lowest to highest
func (*SliceMap) UnmarshalBinary ¶
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SliceMap
func (*SliceMap) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a Map. The JSON must start with an object.
Example ¶
b := []byte(`{"A":"That","B":"This","C":"Other"}`) var m SliceMap json.Unmarshal(b, &m) m.SortByKeys() fmt.Println(&m)
Output: {"A":"That","B":"This","C":"Other"}
func (*SliceMap) Values ¶
func (o *SliceMap) Values() (vals []interface{})
Values returns a slice of the values in the order they were added or sorted.
Example ¶
m := new(SliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Values() fmt.Println(values)
Output: [This That Other]
type StringGetter ¶
type StringLoader ¶
type StringMap ¶
type StringMap struct {
// contains filtered or unexported fields
}
StringMap maps a string to a string. This version is not safe for concurrent use. A zero value is ready for use, but you may not copy it after first using it.
func NewStringMap ¶
func NewStringMap() *StringMap
NewStringMap creates a new map that maps string's to string's.
func NewStringMapFrom ¶
func NewStringMapFrom(i StringMapI) *StringMap
NewStringMapFrom creates a new StringMap from a StringMapI interface object
Example ¶
n := NewStringMap() n.Set("a", "this") n.Set("b", "that") m := NewStringMapFrom(n) fmt.Println(m.Get("b"))
Output: that
func NewStringMapFromMap ¶
NewStringMapFromMap creates a new StringMap from a GO map[string]string object. Note that this will pass control of the given map to the new object. After you do this, DO NOT change the original map.
Example ¶
n := map[string]string{"a": "this", "b": "that"} m := NewStringMapFromMap(n) fmt.Println(m.String())
Output: {"a":"this","b":"that"}
func (*StringMap) Copy ¶
func (o *StringMap) Copy() StringMapI
Copy will make a copy of the map and a copy of the underlying data.
func (*StringMap) Delete ¶
Delete removes the key from the map. If the key does not exist, nothing happens.
func (*StringMap) Equals ¶
func (o *StringMap) Equals(i StringMapI) bool
Equals returns true if all the keys in the given map exist in this map, and the values are the same
Example ¶
m := NewStringMap() m.Set("A", "This") m.Set("B", "That") n := NewStringMap() n.Set("B", "That") n.Set("A", "This") if m.Equals(n) { fmt.Print("Equal") } else { fmt.Print("Not Equal") }
Output: Equal
func (*StringMap) Get ¶
Get returns the value based on its key. If it does not exist, an empty string will be returned.
func (*StringMap) Is ¶ added in v0.3.0
Is returns true if the given key exists in the map and has the given value.
func (*StringMap) Keys ¶
Keys returns a slice of the keys. It will return a nil slice if the map is empty. Multiple calls to Keys will result in the same list of keys, but may be in a different order.
Example ¶
m := NewStringMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Keys() sort.Sort(sort.StringSlice(values)) fmt.Println(values)
Output: [A B C]
func (*StringMap) Load ¶
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load()
func (*StringMap) MarshalBinary ¶
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.
func (*StringMap) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (*StringMap) Merge ¶
func (o *StringMap) Merge(i StringMapI)
Merge merges the given map with the current one. The given one takes precedent on collisions.
Example ¶
m := NewStringMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") n := NewStringMap() n.Set("D", "Last") n.Merge(m) fmt.Println(n.Get("C")) fmt.Println(n.Get("D"))
Output: Other Last
func (*StringMap) MergeMap ¶ added in v0.3.0
MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
Example ¶
m := map[string]string{ "B": "This", "A": "That", "C": "Other", } n := NewStringMap() n.Set("D", "Last") n.MergeMap(m) fmt.Println(n.Get("C")) fmt.Println(n.Get("D"))
Output: Other Last
func (*StringMap) Range ¶
Range will call the given function with every key and value in the map. If f returns false, it stops the iteration. This pattern is taken from sync.Map.
Example ¶
m := NewStringMap() var a []string m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") m.Range(func(key string, val string) bool { a = append(a, val) return true // keep iterating to the end }) fmt.Println() sort.Sort(sort.StringSlice(a)) // unordered maps cannot be guaranteed to range in a particular order. Sort it so we can compare it. fmt.Println(a)
Output: [Other That This]
func (*StringMap) Set ¶
Set sets the key to the given value
Example ¶
m := NewStringMap() m.Set("a", "Here") fmt.Println(m.Get("a")) // Output Here
Output:
func (*StringMap) SetChanged ¶
SetChanged sets the key to the value and returns a boolean indicating whether doing this caused the map to change. It will return true if the key did not first exist, or if the value associated with the key was different than the new value.
func (*StringMap) UnmarshalBinary ¶
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a StringMap
func (*StringMap) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a StringMap. The JSON must start with an object.
func (*StringMap) Values ¶
Values returns a slice of the values. It will return a nil slice if the map is empty. Multiple calls to Values will result in the same list of values, but may be in a different order.
Example ¶
m := NewStringMap() m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Values() sort.Sort(sort.StringSlice(values)) fmt.Println(values)
Output: [Other That This]
type StringMapI ¶
type StringMapI interface { Get(key string) (val string) Has(key string) (exists bool) Values() []string Keys() []string Len() int // Range will iterate over the keys and values in the map. Pattern is taken from sync.Map Range(f func(key string, value string) bool) Merge(i StringMapI) String() string }
The StringMapI interface provides a common interface to the many kinds of similar map objects.
Most functions that change the map are omitted so that you can wrap the map in additional functionality that might use Set or SetChanged. If you want to use them in an interface setting, you can create your own interface that includes them.
type StringSetter ¶
type StringSliceMap ¶
type StringSliceMap struct {
// contains filtered or unexported fields
}
A StringSliceMap combines a map with a slice so that you can range over a map in a predictable order. By default, the order will be the same order that items were inserted, i.e. a FIFO list. This is similar to how PHP arrays work. StringSliceMap implements the sort interface so you can change the order before ranging over the values if desired. It is NOT safe for concurrent use. The zero of this is usable immediately. The StringSliceMap satisfies the StringMapI interface.
func NewStringSliceMap ¶
func NewStringSliceMap() *StringSliceMap
NewStringSliceMap creates a new map that maps string's to string's.
func NewStringSliceMapFrom ¶
func NewStringSliceMapFrom(i StringMapI) *StringSliceMap
NewStringSliceMapFrom creates a new StringMap from a StringMapI interface object
Example ¶
n := new(StringMap) n.Set("a", "this") n.Set("b", "that") m := NewStringSliceMapFrom(n) fmt.Println(m.Get("b"))
Output: that
func NewStringSliceMapFromMap ¶
func NewStringSliceMapFromMap(i map[string]string) *StringSliceMap
NewStringSliceMapFromMap creates a new StringSliceMap from a GO map[string]string object. Note that this will pass control of the given map to the new object. After you do this, DO NOT change the original map.
Example ¶
n := map[string]string{"a": "this", "b": "that"} m := NewStringSliceMapFromMap(n) m.SortByKeys() fmt.Println(m.String())
Output: {"a":"this","b":"that"}
func (*StringSliceMap) Clear ¶
func (o *StringSliceMap) Clear()
func (*StringSliceMap) Copy ¶
func (o *StringSliceMap) Copy() *StringSliceMap
Copy will make a copy of the map and a copy of the underlying data.
func (*StringSliceMap) Delete ¶
func (o *StringSliceMap) Delete(key string)
Delete removes the item with the given key.
Example ¶
n := map[string]string{"a": "this", "b": "that", "c": "other"} m := NewStringSliceMapFromMap(n) m.SortByKeys() m.Delete("b") fmt.Println(m.String())
Output: {"a":"this","c":"other"}
func (*StringSliceMap) Equals ¶
func (o *StringSliceMap) Equals(i StringMapI) bool
Equals returns true if the map equals the given map, paying attention only to the content of the map and not the order.
Example ¶
n := new(StringMap) n.Set("A", "This") n.Set("B", "That") m := NewStringSliceMapFrom(n) if m.Equals(n) { fmt.Println("Equal") } else { fmt.Println("Not Equal") } m.Set("B", "Other") if m.Equals(n) { fmt.Println("Equal") } else { fmt.Println("Not Equal") }
Output: Equal Not Equal
func (*StringSliceMap) Get ¶
func (o *StringSliceMap) Get(key string) (val string)
Get returns the value based on its key. If the key does not exist, an empty value is returned.
func (*StringSliceMap) GetAt ¶
func (o *StringSliceMap) GetAt(position int) (val string)
GetAt returns the value based on its position. If the position is out of bounds, an empty value is returned.
func (*StringSliceMap) GetKeyAt ¶ added in v0.1.5
func (o *StringSliceMap) GetKeyAt(position int) (key string)
GetKeyAt returns the key based on its position. If the position is out of bounds, an empty value is returned.
func (*StringSliceMap) Has ¶
func (o *StringSliceMap) Has(key string) (ok bool)
Has returns true if the given key exists in the map.
func (*StringSliceMap) Is ¶ added in v0.3.0
func (o *StringSliceMap) Is(key string, val string) (is bool)
Is returns true if the given key exists in the map and has the given value.
func (*StringSliceMap) IsNil ¶
func (o *StringSliceMap) IsNil() bool
func (*StringSliceMap) Join ¶
func (o *StringSliceMap) Join(glue string) string
Join is just like strings.Join
func (*StringSliceMap) Keys ¶
func (o *StringSliceMap) Keys() (keys []string)
Keys returns the keys of the map, in the order they were added or sorted
Example ¶
m := new(StringSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Keys() fmt.Println(values)
Output: [B A C]
func (*StringSliceMap) Len ¶
func (o *StringSliceMap) Len() int
Len returns the number of items in the map
func (*StringSliceMap) Load ¶
func (o *StringSliceMap) Load(key string) (val string, ok bool)
Load returns the value based on its key, and a boolean indicating whether it exists in the map. This is the same interface as sync.Map.Load()
func (*StringSliceMap) MarshalBinary ¶
func (o *StringSliceMap) MarshalBinary() (data []byte, err error)
MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream. If you are using a sort function, you must save and restore the sort function in a separate operation since functions are not serializable.
func (*StringSliceMap) MarshalJSON ¶
func (o *StringSliceMap) MarshalJSON() (data []byte, err error)
MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
Example ¶
// You don't normally call MarshallJSON directly, but rather use the Marshall and Unmarshall json commands m := new(StringSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") s, _ := json.Marshal(m) os.Stdout.Write(s) // Note: The below output is what is produced, but isn't guaranteed. go seems to currently be sorting keys
Output:
func (*StringSliceMap) Merge ¶
func (o *StringSliceMap) Merge(i StringMapI)
Merge the given map into the current one
Example ¶
m := new(StringSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") n := new(StringSliceMap) n.SortByKeys() n.Set("D", "Last") n.Merge(m) values := n.Values() fmt.Println(values)
Output: [That This Other Last]
func (*StringSliceMap) MergeMap ¶ added in v0.3.0
func (o *StringSliceMap) MergeMap(m map[string]string)
MergeMap merges the given standard map with the current one. The given one takes precedent on collisions.
Example ¶
m := map[string]string{ "B": "This", "A": "That", "C": "Other", } n := NewStringSliceMap() n.SortByKeys() n.Set("D", "Last") n.MergeMap(m) values := n.Values() fmt.Println(values)
Output: [That This Other Last]
func (*StringSliceMap) Range ¶
func (o *StringSliceMap) Range(f func(key string, value string) bool)
Range will call the given function with every key and value in the order they were placed in the map, or in if you sorted the map, in your custom order. If f returns false, it stops the iteration. This pattern is taken from sync.Map.
Example ¶
m := new(StringSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") // Iterate by insertion order m.Range(func(key string, val string) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println() m.SortByValues() m.Set("D", "Other2") // test adding value after sorting m.Range(func(key string, val string) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println() m.SortByKeys() m.Range(func(key string, val string) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println()
Output: B:This,A:That,C:Other, C:Other,D:Other2,A:That,B:This, A:That,B:This,C:Other,D:Other2,
func (*StringSliceMap) Set ¶
func (o *StringSliceMap) Set(key string, val string)
Set sets the given key to the given value. If the key already exists, the range order will not change.
func (*StringSliceMap) SetAt ¶
func (o *StringSliceMap) SetAt(index int, key string, val string)
SetAt sets the given key to the given value, but also inserts it at the index specified. If the index is bigger than the length, it puts it at the end. Negative indexes are backwards from the end.
func (*StringSliceMap) SetChanged ¶
func (o *StringSliceMap) SetChanged(key string, val string) (changed bool)
SetChanged sets the value. It returns true if something in the map changed. If the key was already in the map, and you have not provided a sort function, the order will not change, but the value will be replaced. If you wanted the order to change, you must Delete then call SetChanged. If you have previously set a sort function, the order will be updated.
func (*StringSliceMap) SetSortFunc ¶ added in v0.2.0
func (o *StringSliceMap) SetSortFunc(f func(key1, key2 string, val1, val2 string) bool) *StringSliceMap
SetSortFunc sets the sort function which will determine the order of the items in the map on an ongoing basis. Normally, items will iterate in the order they were added. The sort function is a Less function, that returns true when item 1 is "less" than item 2. The sort function receives both the keys and values, so it can use either to decide how to sort.
func (*StringSliceMap) SortByKeys ¶ added in v0.2.0
func (o *StringSliceMap) SortByKeys() *StringSliceMap
SortByKeys sets up the map to have its sort order sort by keys, lowest to highest
func (*StringSliceMap) SortByValues ¶ added in v0.2.0
func (o *StringSliceMap) SortByValues()
SortByValues sets up the map to have its sort order sort by values, lowest to highest
func (*StringSliceMap) String ¶
func (o *StringSliceMap) String() string
func (*StringSliceMap) UnmarshalBinary ¶
func (o *StringSliceMap) UnmarshalBinary(data []byte) (err error)
UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a StringSliceMap
func (*StringSliceMap) UnmarshalJSON ¶
func (o *StringSliceMap) UnmarshalJSON(data []byte) (err error)
UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a StringMap. The JSON must start with an object.
Example ¶
b := []byte(`{"A":"That","B":"This","C":"Other"}`) var m StringSliceMap json.Unmarshal(b, &m) m.SortByKeys() fmt.Println(&m)
Output: {"A":"That","B":"This","C":"Other"}
func (*StringSliceMap) Values ¶
func (o *StringSliceMap) Values() (vals []string)
Values returns a slice of the values in the order they were added or sorted.
Example ¶
m := new(StringSliceMap) m.Set("B", "This") m.Set("A", "That") m.Set("C", "Other") values := m.Values() fmt.Println(values)
Output: [This That Other]