import "bitbucket.org/gosimple/listdict"
Python List and Dict for Go
This package bring list and dict to Go with most methods you can find in Python.
dict := listdict.Dict{"one": 1, "two": 2, "three": 3}
if dict.HasKey("one") {
// Do something if dict have key "one"
}
keys := dict.Keys() // keys = [one two three]
val := dict.Values() // val = [3 1 2]
// Keys() and Values() are unordered, same as in Python
Requests or bugs? https://bitbucket.org/gosimple/listdict/issues
var (
// ErrRemoveFromEmptyDict is returned when user want to remove element
// from empty dictionary
ErrRemoveFromEmptyDict = errors.
New("Trying to remove element from empty dict")
)var (
// ErrRemoveFromEmptyList is returned when user want to remove element
// from empty list
ErrRemoveFromEmptyList = errors.
New("Trying to remove element from empty list")
)
type Dict map[string]interface{}
Simple dict.
func DictFromKeys(list List, defaultVal interface{}) Dict
DictFromKeys creates a new dictionary with keys from list and values set to defaultVal.
func NewDict() Dict
Return new Dict.
func (dict Dict) Clear()
Clear removes all elements from the dictionary.
func (dict Dict) Get(key string, defaultVal interface{}) interface{}
Get returns value for the given key or defaultVal if key is NOT in the dictionary. defaultVal should be same type as you expect to get.
d := listdict.Dict{"one": 1, "two": 2}
d["one"] => 1
d.Get("one", 4) => 1
d["three"] => error
d.Get("three", 3) => 3
// d = {'one': 1, 'two': 2}
func (dict Dict) HasKey(key string) bool
HasKey returns true if key is in the dictionary, false otherwise.
func (dict Dict) IsEqual(otherDict Dict) bool
IsEqual returns true if dicts are equal.
func (dict Dict) Items() []List
Items returns an unordered list of the dictionary's [key, value] pairs.
func (dict Dict) Keys() List
Keys returns a list of the dictionary's keys, unordered.
func (dict Dict) Pop(key string, defaultVal interface{}) (interface{}, error)
Pop returns value and remove the given key from the dictionary. If the given key is NOT in the dictionary return defaultVal. defaultVal should be same type as you expect to get.
func (dict Dict) PopItem() (List, error)
PopItem return and remove a random key-value pair as List from the dictionary.
func (dict Dict) SetDefault(key string, defaultVal interface{}) interface{}
SetDefault is like Get but will set dict[key] to defaultVal if key is not already in dict. defaultVal should be same type as you expect to get.
d := listdict.Dict{"one": 1, "two": 2}
d["one"] => 1
d.SetDefault("one", 4) => 1
d["three"] => error
d.SetDefault("three", 3) => 3
// d = {'one': 1, 'two': 2, 'three': 3}
func (dict Dict) Update(dict2 Dict)
Update updates the dictionary with the key-value pairs in the dict2 dictionary replacing current values and adding new if found.
func (dict Dict) Values() List
Values returns a list of the dictionary's values, unordered.
type List []interface{}Simple list
func NewList(length int) List
Return new List with specified length
func (list *List) Append(values ...interface{})
Append adds an element to the end of the list.
func (list *List) AppendIfMissing(value interface{})
AppendIfMissing adds an element to the end of the list if it's not already in the list.
func (list List) Count(value interface{}) int
Count returns the number of times value appears in the list.
func (list *List) Delete(index int) error
Delete removes element with given index from the list.
func (list *List) Extend(otherList List)
Extend one list with the contents of the other list.
func (list List) Index(val interface{}) (int, error)
Index returns the index of the first item in the list whose value is val. It is -1 if there is no such item.
func (list *List) Insert(index int, values ...interface{})
Insert an element at a given position. If the position is past the end of the list, append to the end.
func (list List) IsEqual(otherList List) bool
IsEqual returns true if lists are equal.
func (list *List) Pop() (interface{}, error)
Remove and returns the last element in the list.
func (list *List) PopItem(index int) (interface{}, error)
Remove and returns the element at the given position in the list.
func (list *List) Remove(val interface{}) error
Remove the first element from the list whose value matches the given value. Error if no match is found.
func (list *List) Reverse()
Reverse the elements of the list in place.
func (list List) String() string
String returns list values as string
l := listdict.List{"one", 2, "three"}
l.String() => "one, 2, three"