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

type Dict map[string]interface{}

Simple dict.

func DictFromKeys

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

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

func NewDict

func NewDict() Dict

Return new Dict.

func (Dict) Clear

func (dict Dict) Clear()

Clear removes all elements from the dictionary.

func (Dict) Get

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

func (dict Dict) HasKey(key string) bool

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

func (Dict) IsEqual

func (dict Dict) IsEqual(otherDict Dict) bool

IsEqual returns true if dicts are equal.

func (Dict) Items

func (dict Dict) Items() []List

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

func (Dict) Keys

func (dict Dict) Keys() List

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

func (Dict) Pop

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

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

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

func (Dict) SetDefault

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

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

func (dict Dict) Values() List

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

type List

type List []interface{}

Simple list

func NewList

func NewList(length int) List

Return new List with specified length

func (*List) Append

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

Append adds an element to the end of the list.

func (*List) AppendIfMissing

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

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

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

func (*List) Delete

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

Delete removes element with given index from the list.

func (*List) Extend

func (list *List) Extend(otherList List)

Extend one list with the contents of the other list.

func (List) Index

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

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

func (list List) IsEqual(otherList List) bool

IsEqual returns true if lists are equal.

func (*List) Pop

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

Remove and returns the last element in the list.

func (*List) PopItem

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

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

func (*List) Remove

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

func (list *List) Reverse()

Reverse the elements of the list in place.

func (List) String

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 2014-06-17. Refresh now. Tools for package owners.