Documentation
¶
Index ¶
- func OrderSafeSliceMapByKeys(o *SafeSliceMap) sort.Interface
- func OrderSafeStringSliceMapByKeys(o *SafeStringSliceMap) sort.Interface
- func OrderSliceMapByKeys(o *SliceMap) sort.Interface
- func OrderStringSliceMapByKeys(o *StringSliceMap) sort.Interface
- type Comparer
- type Copier
- 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) Range(f func(key string, value interface{}) bool)
- func (o *Map) Set(key string, val interface{})
- func (o *Map) SetChanged(key string, val interface{}) (changed bool)
- 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) Range(f func(key string, value interface{}) bool)
- func (o *SafeMap) Set(key string, val interface{})
- func (o *SafeMap) SetChanged(key string, val interface{}) (changed bool)
- 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) Has(key string) (ok bool)
- func (o *SafeSliceMap) IsNil() bool
- func (o *SafeSliceMap) Keys() (keys []string)
- func (o *SafeSliceMap) Len() int
- func (o *SafeSliceMap) Less(i, j int) bool
- 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) 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) SetChanged(key string, val interface{}) (changed bool)
- func (o *SafeSliceMap) String() string
- func (o *SafeSliceMap) Swap(i, j int)
- 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) 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) 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) 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) Has(key string) (ok 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) Less(i, j int) bool
- 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) 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) String() string
- func (o *SafeStringSliceMap) Swap(i, j int)
- 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) Has(key string) (ok bool)
- func (o *SliceMap) IsNil() bool
- func (o *SliceMap) Keys() (keys []string)
- func (o *SliceMap) Len() int
- func (o *SliceMap) Less(i, j int) bool
- 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) 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) SetChanged(key string, val interface{}) (changed bool)
- func (o *SliceMap) String() string
- func (o *SliceMap) Swap(i, j int)
- func (o *SliceMap) UnmarshalBinary(data []byte) (err error)
- func (o *SliceMap) UnmarshalJSON(data []byte) (err error)
- func (o *SliceMap) Values() (vals []interface{})
- type StringComparer
- type StringCopier
- 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) 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) 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) 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) Has(key string) (ok 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) Less(i, j int) bool
- 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) 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) String() string
- func (o *StringSliceMap) Swap(i, j int)
- 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.Range
- Map.Set
- Map.UnmarshalJSON
- Map.Values
- NewMapFrom
- NewSafeMapFrom
- NewSafeSliceMapFrom
- NewSafeStringMapFrom
- NewSafeStringSliceMapFrom
- NewSliceMapFrom
- NewStringMapFrom
- NewStringSliceMapFrom
- SafeMap.Equals
- SafeMap.Keys
- SafeMap.MarshalBinary
- SafeMap.MarshalJSON
- SafeMap.Merge
- SafeMap.Range
- SafeMap.Set
- SafeMap.UnmarshalJSON
- SafeMap.Values
- SafeSliceMap.Equals
- SafeSliceMap.Keys
- SafeSliceMap.MarshalBinary
- SafeSliceMap.MarshalJSON
- SafeSliceMap.Merge
- SafeSliceMap.Range
- SafeSliceMap.UnmarshalJSON
- SafeSliceMap.Values
- SafeStringMap.Equals
- SafeStringMap.Keys
- SafeStringMap.Merge
- SafeStringMap.Range
- SafeStringMap.Set
- SafeStringMap.Values
- SafeStringSliceMap.Equals
- SafeStringSliceMap.Keys
- SafeStringSliceMap.MarshalBinary
- SafeStringSliceMap.MarshalJSON
- SafeStringSliceMap.Merge
- SafeStringSliceMap.Range
- SafeStringSliceMap.UnmarshalJSON
- SafeStringSliceMap.Values
- SliceMap.Equals
- SliceMap.Keys
- SliceMap.MarshalBinary
- SliceMap.MarshalJSON
- SliceMap.Merge
- SliceMap.Range
- SliceMap.UnmarshalJSON
- SliceMap.Values
- StringMap.Equals
- StringMap.Keys
- StringMap.Merge
- StringMap.Range
- StringMap.Set
- StringMap.Values
- StringSliceMap.Equals
- StringSliceMap.Keys
- StringSliceMap.MarshalBinary
- StringSliceMap.MarshalJSON
- StringSliceMap.Merge
- StringSliceMap.Range
- StringSliceMap.UnmarshalJSON
- StringSliceMap.Values
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OrderSafeSliceMapByKeys ¶
func OrderSafeSliceMapByKeys(o *SafeSliceMap) sort.Interface
A helper function to allow SafeSliceMaps to be sorted by keys To sort the map by keys, call:
sort.Sort(OrderSafeStringSliceMapByKeys(m))
func OrderSafeStringSliceMapByKeys ¶
func OrderSafeStringSliceMapByKeys(o *SafeStringSliceMap) sort.Interface
A helper function to allow SafeStringSliceMaps to be sorted by keys To sort the map by keys, call:
sort.Sort(OrderSafeStringStringSliceMapByKeys(m))
func OrderSliceMapByKeys ¶
A helper function to allow SliceMaps to be sorted by keys To sort the map by keys, call:
sort.Sort(OrderStringSliceMapByKeys(m))
func OrderStringSliceMapByKeys ¶
func OrderStringSliceMapByKeys(o *StringSliceMap) sort.Interface
A helper function to allow StringSliceMaps to be sorted by keys To sort the map by keys, call:
sort.Sort(OrderStringStringSliceMapByKeys(m))
Types ¶
type Comparer ¶
type Comparer interface { // Compare should return a value less than 0 if the receiver is less than the given value, // 0 if equal, and a value greater than 0 if the receiver is greater than the given value. Compare(interface{}) int }
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) Copy ¶
Copy will make a copy of the map and a copy of the underlying data. If the values implement the Copier interface, the value's Copy function will be called to deep copy the items.
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) m.Merge(n) fmt.Println(m.Get("D"))
Output: 5
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.Get("a"))
Output: Here
func (*Map) 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 (*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) }
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) Copy ¶
Copy will make a copy of the map and a copy of the underlying data. If the values implement the Copier interface, the value's Copy function will be called to deep copy the items.
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) m.Merge(n) fmt.Println(m.Get("D"))
Output: 5
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.Get("a"))
Output: Here
func (*SafeMap) 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 (*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. If the values implement the Copier interface, the value's Copy function will be called to deep copy the items.
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) 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) Less ¶
func (o *SafeSliceMap) Less(i, j int) bool
Less is part of the interface that allows the map to be sorted by values. It returns true if the value at position i should be sorted before the value at position j.
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.
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", "Other") n := new(Map) n.Set("D", "Last") m.Merge(n) fmt.Println(m.GetAt(3))
Output: 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 sort.Sort(OrderSafeSliceMapByKeys(m)) 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,
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, or -1, it is the same as Set, in that it puts it at the end. Negative indexes are backwards from the end, if smaller than the negative length, just inserts at the beginning.
func (*SafeSliceMap) SetChanged ¶
func (o *SafeSliceMap) SetChanged(key string, val interface{}) (changed bool)
SetChanged sets the value, but also appends the value to the end of the list. It returns true if something in the map changed. If the key was already in the map, the order will not change, but the value will be replaced. If you want the order to change, you must Delete then call SetChanged.
func (*SafeSliceMap) String ¶
func (o *SafeSliceMap) String() string
func (*SafeSliceMap) Swap ¶
func (o *SafeSliceMap) Swap(i, j int)
Swap is part of the interface that allows the slice to be sorted. It swaps the positions of the items and position i and j.
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) sort.Sort(OrderSafeSliceMapByKeys(&m)) 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.
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) 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") m.Merge(n) fmt.Println(m.Get("D"))
Output: 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 := NewStringMap() 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) 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 := 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 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.
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.
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.Print("Equal") } else { fmt.Print("Not Equal") }
Output: 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) Has ¶
func (o *SafeStringSliceMap) Has(key string) (ok bool)
Has returns true if the given key exists in the map.
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) Less ¶
func (o *SafeStringSliceMap) Less(i, j int) bool
Less is part of the interface that allows the map to be sorted by values. It returns true if the value at position i should be sorted before the value at position j.
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.
Example ¶
// You would rarely call MarshallBinary directly, but rather would use an encoder, like GOB for binary encoding m := new(SafeStringSliceMap) var m2 SafeStringSliceMap 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 (*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(StringMap) n.Set("D", "Last") m.Merge(n) fmt.Println(m.GetAt(3))
Output: 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() // Iterate after sorting values sort.Sort(m) m.Range(func(key string, val string) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println() // Iterate after sorting keys sort.Sort(OrderSafeStringSliceMapByKeys(m)) 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,A:That,B:This, A:That,B:This,C:Other,
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, or -1, it is the same as Set, in that it puts it at the end. Negative indexes are backwards from the end, if smaller than the negative length, just inserts at the beginning.
func (*SafeStringSliceMap) SetChanged ¶
func (o *SafeStringSliceMap) SetChanged(key string, val string) (changed bool)
SetChanged sets the value, but also appends the value to the end of the list. It returns true if something in the map changed. If the key was already in the map, the order will not change, but the value will be replaced. If you want the order to change, you must Delete then call SetChanged.
func (*SafeStringSliceMap) String ¶
func (o *SafeStringSliceMap) String() string
func (*SafeStringSliceMap) Swap ¶
func (o *SafeStringSliceMap) Swap(i, j int)
Swap is part of the interface that allows the slice to be sorted. It swaps the positions of the items and position i and j.
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) sort.Sort(OrderSafeStringSliceMapByKeys(&m)) 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) Copy ¶
Copy will make a copy of the map and a copy of the underlying data. If the values implement the Copier interface, the value's Copy function will be called to deep copy the items.
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) 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) Less ¶
Less is part of the interface that allows the map to be sorted by values. It returns true if the value at position i should be sorted before the value at position j.
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.
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", "Other") n := new(Map) n.Set("D", "Last") m.Merge(n) fmt.Println(m.GetAt(3))
Output: 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 sort.Sort(OrderSliceMapByKeys(m)) 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,
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, or -1, it is the same as Set, in that it puts it at the end. Negative indexes are backwards from the end, if smaller than the negative length, just inserts at the beginning.
func (*SliceMap) SetChanged ¶
SetChanged sets the value, but also appends the value to the end of the list. It returns true if something in the map changed. If the key was already in the map, the order will not change, but the value will be replaced. If you want the order to change, you must Delete then call SetChanged.
func (*SliceMap) Swap ¶
Swap is part of the interface that allows the slice to be sorted. It swaps the positions of the items and position i and j.
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) sort.Sort(OrderSliceMapByKeys(&m)) 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 StringComparer ¶
type StringCopier ¶
type StringCopier interface {
Copy() string
}
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.
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) 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") m.Merge(n) fmt.Println(m.Get("D"))
Output: 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() 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) }
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.
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.
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.Print("Equal") } else { fmt.Print("Not Equal") }
Output: 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) Has ¶
func (o *StringSliceMap) Has(key string) (ok bool)
Has returns true if the given key exists in the map.
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) Less ¶
func (o *StringSliceMap) Less(i, j int) bool
Less is part of the interface that allows the map to be sorted by values. It returns true if the value at position i should be sorted before the value at position j.
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.
Example ¶
// You would rarely call MarshallBinary directly, but rather would use an encoder, like GOB for binary encoding m := new(StringSliceMap) var m2 StringSliceMap 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 (*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(StringMap) n.Set("D", "Last") m.Merge(n) fmt.Println(m.GetAt(3))
Output: 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() // Iterate after sorting values sort.Sort(m) m.Range(func(key string, val string) bool { fmt.Printf("%s:%s,", key, val) return true // keep iterating to the end }) fmt.Println() // Iterate after sorting keys sort.Sort(OrderStringSliceMapByKeys(m)) 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,A:That,B:This, A:That,B:This,C:Other,
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, or -1, it is the same as Set, in that it puts it at the end. Negative indexes are backwards from the end, if smaller than the negative length, just inserts at the beginning.
func (*StringSliceMap) SetChanged ¶
func (o *StringSliceMap) SetChanged(key string, val string) (changed bool)
SetChanged sets the value, but also appends the value to the end of the list. It returns true if something in the map changed. If the key was already in the map, the order will not change, but the value will be replaced. If you want the order to change, you must Delete then call SetChanged.
func (*StringSliceMap) String ¶
func (o *StringSliceMap) String() string
func (*StringSliceMap) Swap ¶
func (o *StringSliceMap) Swap(i, j int)
Swap is part of the interface that allows the slice to be sorted. It swaps the positions of the items and position i and j.
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) sort.Sort(OrderStringSliceMapByKeys(&m)) 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]