listdict: bitbucket.org/gosimple/listdict Index | Files

package listdict

import "bitbucket.org/gosimple/listdict"

Python List and Dict for Go

gosimple/listdict has moved to Github, please visit:

https://github.com/gosimple/listdict

for current source code.

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

Index

Package Files

dict.go doc.go list.go

Variables

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 Uses

type Dict map[string]interface{}

Simple dict.

func DictFromKeys Uses

func DictFromKeys(list List, defaultVal interface{}) Dict

DictFromKeys creates a new dictionary with keys from list and values set to defaultVal.

func NewDict Uses

func NewDict() Dict

Return new Dict.

func (Dict) Clear Uses

func (dict Dict) Clear()

Clear removes all elements from the dictionary.

func (Dict) Get Uses

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) HasKey Uses

func (dict Dict) HasKey(key string) bool

HasKey returns true if key is in the dictionary, false otherwise.

func (Dict) IsEqual Uses

func (dict Dict) IsEqual(otherDict Dict) bool

IsEqual returns true if dicts are equal.

func (Dict) Items Uses

func (dict Dict) Items() []List

Items returns an unordered list of the dictionary's [key, value] pairs.

func (Dict) Keys Uses

func (dict Dict) Keys() List

Keys returns a list of the dictionary's keys, unordered.

func (Dict) Pop Uses

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) PopItem Uses

func (dict Dict) PopItem() (List, error)

PopItem return and remove a random key-value pair as List from the dictionary.

func (Dict) SetDefault Uses

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) Update Uses

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) Values Uses

func (dict Dict) Values() List

Values returns a list of the dictionary's values, unordered.

type List Uses

type List []interface{}

Simple list

func NewList Uses

func NewList(length int) List

Return new List with specified length

func (*List) Append Uses

func (list *List) Append(values ...interface{})

Append adds an element to the end of the list.

func (*List) AppendIfMissing Uses

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) Count Uses

func (list List) Count(value interface{}) int

Count returns the number of times value appears in the list.

func (*List) Delete Uses

func (list *List) Delete(index int) error

Delete removes element with given index from the list.

func (*List) Extend Uses

func (list *List) Extend(otherList List)

Extend one list with the contents of the other list.

func (List) Index Uses

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) Insert Uses

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) IsEqual Uses

func (list List) IsEqual(otherList List) bool

IsEqual returns true if lists are equal.

func (*List) Pop Uses

func (list *List) Pop() (interface{}, error)

Remove and returns the last element in the list.

func (*List) PopItem Uses

func (list *List) PopItem(index int) (interface{}, error)

Remove and returns the element at the given position in the list.

func (*List) Remove Uses

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) Reverse Uses

func (list *List) Reverse()

Reverse the elements of the list in place.

func (List) String Uses

func (list List) String() string

String returns list values as string

l := listdict.List{"one", 2, "three"}
l.String() => "one, 2, three"

Package listdict imports 6 packages (graph). Updated 2016-07-15. Refresh now. Tools for package owners.