https://blog.golang.org/go-maps-in-action
map === dictionary
mapping of key-value pairs
var m0 = map[string]int{
"tsm": 2330,
"umc": 2303,
"cmc": 2323,
"csc": 2002,
}
var m1 map[string]int //m1 is nil
var m2 = map[string]int{} //m2 points to a map with default initialization (zero values)
var m3 = make(map[string]int) // m3 also points to a map with default initialization
"""...
map[KeyType]ValueType
var m map[string]int
...
Map types are reference type
... the value of m above is nil; it doesn't point to an initialized map.
... A nil map behaves like an empty map when reading, but attempts to write to a nil map will cause a runtime panic; ...
... To initialize a map, use the built in make function:
m = make(map[string]int)
...
i, ok := m["route"]
the first value (i) is assigned the value stored under the key "route".
If that key doesn't exist, i is the value type's zero value (0).
The second value (ok) is a bool that is true if the key exists in the map, and false if not.
To test for a key without retrieving the value, use an underscore in place of the first value:
_, ok := m["route"]
To iterate over the contents of a map, use the range keyword:
for key, value := range m {
fmt.Println("Key:", key, "Value:", value)
}
..."""
[ex] swarm/filter.go
type Filter interface {
// Return a subset of nodes that were accepted by the filtering policy.
Filter(*dockerclient.ContainerConfig, []cluster.Node) ([]cluster.Node, error)
}
var (
filters map[string]Filter
ErrNotSupported = errors.New("filter not supported")
)
2015年3月30日 星期一
golang map type
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言