maps

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2019 License: MIT Imports: 7 Imported by: 9

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Getter

type Getter interface {
	Get(key string) (val interface{})
}

type Loader

type Loader interface {
	Load(key string) (val interface{}, ok bool)
}

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 NewMap

func NewMap() *Map

NewMap creates a new map that maps string's to interface{}'s.

func NewMapFrom

func NewMapFrom(i MapI) *Map

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

func NewMapFromMap(i map[string]interface{}) *Map

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) Clear

func (o *Map) Clear()

Clear resets the map to an empty map

func (*Map) Copy

func (o *Map) Copy() MapI

Copy will make a copy of the map and a copy of the underlying data.

func (*Map) Delete

func (o *Map) Delete(key string)

Delete removes the key from the map. If the key does not exist, nothing happens.

func (*Map) Equals

func (o *Map) Equals(i MapI) bool

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

func (o *Map) Get(key string) (val interface{})

Get returns the value based on its key. If it does not exist, an empty string will be returned.

func (*Map) Has

func (o *Map) Has(key string) (exists bool)

Has returns true if the given key exists in the map.

func (*Map) IsNil

func (o *Map) IsNil() bool

func (*Map) Keys

func (o *Map) 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 := 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) Len

func (o *Map) Len() (l int)

Len returns the number of items in the map

func (*Map) Load

func (o *Map) 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 (*Map) LoadBool

func (o *Map) LoadBool(key string) (val bool, ok bool)

func (*Map) LoadFloat64

func (o *Map) LoadFloat64(key string) (val float64, ok bool)

func (*Map) LoadInt

func (o *Map) LoadInt(key string) (val int, ok bool)

func (*Map) LoadString

func (o *Map) LoadString(key string) (val string, ok bool)

func (*Map) MarshalBinary

func (o *Map) MarshalBinary() ([]byte, 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(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

func (o *Map) MarshalJSON() (out []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(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

func (o *Map) Merge(i MapI)

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

func (o *Map) 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": 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

func (o *Map) Range(f func(key string, value interface{}) 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.

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

func (o *Map) Set(key string, val interface{})

Set sets the key to the given value

Example
m := NewMap()
m.Set("a", "Here")
fmt.Println(m.String())
Output:

{"a":"Here"}

func (*Map) String added in v0.2.0

func (o *Map) String() string

func (*Map) UnmarshalBinary

func (o *Map) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a Map

func (*Map) UnmarshalJSON

func (o *Map) UnmarshalJSON(in []byte) (err error)

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

type SafeMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

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

func NewSafeMapFrom(i MapI) *SafeMap

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

func NewSafeMapFromMap(i map[string]interface{}) *SafeMap

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) Clear

func (o *SafeMap) Clear()

Clear resets the map to an empty map

func (*SafeMap) Copy

func (o *SafeMap) Copy() MapI

Copy will make a copy of the map and a copy of the underlying data.

func (*SafeMap) Delete

func (o *SafeMap) Delete(key string)

Delete removes the key from the map. If the key does not exist, nothing happens.

func (*SafeMap) Equals

func (o *SafeMap) Equals(i MapI) bool

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

func (o *SafeMap) Get(key string) (val interface{})

Get returns the value based on its key. If it does not exist, an empty string will be returned.

func (*SafeMap) Has

func (o *SafeMap) Has(key string) (exists bool)

Has returns true if the given key exists in the map.

func (*SafeMap) IsNil

func (o *SafeMap) IsNil() bool

func (*SafeMap) Keys

func (o *SafeMap) 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 := 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) Len

func (o *SafeMap) Len() (l int)

Len returns the number of items in the map

func (*SafeMap) Load

func (o *SafeMap) 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 (*SafeMap) LoadBool

func (o *SafeMap) LoadBool(key string) (val bool, ok bool)

func (*SafeMap) LoadFloat64

func (o *SafeMap) LoadFloat64(key string) (val float64, ok bool)

func (*SafeMap) LoadInt

func (o *SafeMap) LoadInt(key string) (val int, ok bool)

func (*SafeMap) LoadString

func (o *SafeMap) LoadString(key string) (val string, ok bool)

func (*SafeMap) MarshalBinary

func (o *SafeMap) MarshalBinary() ([]byte, 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(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

func (o *SafeMap) MarshalJSON() (out []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(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

func (o *SafeMap) Merge(i MapI)

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

func (o *SafeMap) 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": 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

func (o *SafeMap) Range(f func(key string, value interface{}) 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 := 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

func (o *SafeMap) Set(key string, val interface{})

Set sets the key to the given value

Example
m := NewSafeMap()
m.Set("a", "Here")
fmt.Println(m.String())
Output:

{"a":"Here"}

func (*SafeMap) String added in v0.2.0

func (o *SafeMap) String() string

func (*SafeMap) UnmarshalBinary

func (o *SafeMap) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SafeMap

func (*SafeMap) UnmarshalJSON

func (o *SafeMap) UnmarshalJSON(in []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":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

type SafeSliceMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

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) LoadBool

func (o *SafeSliceMap) LoadBool(key string) (val bool, ok bool)

func (*SafeSliceMap) LoadFloat64

func (o *SafeSliceMap) LoadFloat64(key string) (val float64, ok bool)

func (*SafeSliceMap) LoadInt

func (o *SafeSliceMap) LoadInt(key string) (val int, 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

type SafeStringMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

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) Clear

func (o *SafeStringMap) Clear()

Clear resets the map to an empty 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) 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

type SafeStringSliceMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

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

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 Setter

type Setter interface {
	Set(string, interface{})
}

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

func NewSliceMapFrom(i MapI) *SliceMap

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

func NewSliceMapFromMap(i map[string]interface{}) *SliceMap

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) Clear

func (o *SliceMap) Clear()

func (*SliceMap) Copy

func (o *SliceMap) Copy() *SliceMap

Copy will make a copy of the map and a copy of the underlying data.

func (*SliceMap) Delete

func (o *SliceMap) Delete(key string)

Delete removes the item with the given key.

func (*SliceMap) Equals

func (o *SliceMap) 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 := NewSliceMapFrom(n)
if m.Equals(n) {
	fmt.Print("Equal")
} else {
	fmt.Print("Not Equal")
}
Output:

Equal

func (*SliceMap) Get

func (o *SliceMap) 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 (*SliceMap) GetAt

func (o *SliceMap) 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 (*SliceMap) GetKeyAt added in v0.1.5

func (o *SliceMap) 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 (*SliceMap) Has

func (o *SliceMap) Has(key string) (ok bool)

Has returns true if the given key exists in the map.

func (*SliceMap) IsNil

func (o *SliceMap) IsNil() bool

func (*SliceMap) Keys

func (o *SliceMap) Keys() (keys []string)

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) Len

func (o *SliceMap) Len() int

Len returns the number of items in the map

func (*SliceMap) Load

func (o *SliceMap) 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 (*SliceMap) LoadBool

func (o *SliceMap) LoadBool(key string) (val bool, ok bool)

func (*SliceMap) LoadFloat64

func (o *SliceMap) LoadFloat64(key string) (val float64, ok bool)

func (*SliceMap) LoadInt

func (o *SliceMap) LoadInt(key string) (val int, ok bool)

func (*SliceMap) LoadString

func (o *SliceMap) LoadString(key string) (val string, ok bool)

func (*SliceMap) MarshalBinary

func (o *SliceMap) 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(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

func (o *SliceMap) 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(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

func (o *SliceMap) Merge(i MapI)

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

func (o *SliceMap) 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 := NewSliceMap()
n.SortByKeys()
n.Set("D", "Last")
n.MergeMap(m)
values := n.Values()
fmt.Println(values)
Output:

[That This 5 Last]

func (*SliceMap) Range

func (o *SliceMap) 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(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

func (o *SliceMap) 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 (*SliceMap) SetAt

func (o *SliceMap) 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 (*SliceMap) SetSortFunc added in v0.2.0

func (o *SliceMap) SetSortFunc(f func(key1, key2 string, val1, val2 interface{}) bool) *SliceMap

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

func (o *SliceMap) SortByKeys() *SliceMap

SortByKeys sets up the map to have its sort order sort by keys, lowest to highest

func (*SliceMap) String

func (o *SliceMap) String() string

func (*SliceMap) UnmarshalBinary

func (o *SliceMap) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a SliceMap

func (*SliceMap) UnmarshalJSON

func (o *SliceMap) UnmarshalJSON(data []byte) (err error)

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 StringGetter interface {
	Get(key string) (val string)
}

type StringLoader

type StringLoader interface {
	Load(key string) (val string, ok bool)
}

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

func NewStringMapFromMap(i map[string]string) *StringMap

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) Clear

func (o *StringMap) Clear()

Clear resets the map to an empty 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

func (o *StringMap) Delete(key string)

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

func (o *StringMap) 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 (*StringMap) Has

func (o *StringMap) Has(key string) (exists bool)

Has returns true if the given key exists in the map.

func (*StringMap) Is added in v0.3.0

func (o *StringMap) Is(key string, val string) (is bool)

Is returns true if the given key exists in the map and has the given value.

func (*StringMap) IsNil

func (o *StringMap) IsNil() bool

func (*StringMap) Keys

func (o *StringMap) 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 := 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) Len

func (o *StringMap) Len() (l int)

Len returns the number of items in the map

func (*StringMap) Load

func (o *StringMap) 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 (*StringMap) MarshalBinary

func (o *StringMap) MarshalBinary() ([]byte, error)

MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.

func (*StringMap) MarshalJSON

func (o *StringMap) MarshalJSON() (out []byte, err error)

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

func (o *StringMap) 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 := NewStringMap()
n.Set("D", "Last")
n.MergeMap(m)

fmt.Println(n.Get("C"))
fmt.Println(n.Get("D"))
Output:

Other
Last

func (*StringMap) Range

func (o *StringMap) 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.

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

func (o *StringMap) Set(key string, val string)

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

func (o *StringMap) 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 (*StringMap) String added in v0.2.0

func (o *StringMap) String() string

func (*StringMap) UnmarshalBinary

func (o *StringMap) UnmarshalBinary(data []byte) (err error)

UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a StringMap

func (*StringMap) UnmarshalJSON

func (o *StringMap) UnmarshalJSON(in []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a StringMap. The JSON must start with an object.

func (*StringMap) Values

func (o *StringMap) 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 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 StringSetter interface {
	Set(string, string)
}

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]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL