Maps
Overview
Like many other modern languages, V has built-in support for maps (sometimes called hashes or dicts in other languages). Map is an associative data type that stores key-value pairs.
Currently, maps can have keys of type string
, rune
, integers, floats, enums or voidptr
.
Maps are ordered by insertion, like dictionaries in Python. The order is a guaranteed language feature. This may change in the future.
See all methods of
map
struct
and
maps
module.
Map Initialization
There are two syntaxes for creating a map.
The first one is used to create an empty map:
The square brackets indicate the type of the key, followed by the type of the value.
The second syntax is used to create a map with initial values:
V automatically infers the type of the key and value, in the example above the map will be of
type map[string]int
.
Add elements to a map
To add an element to the map, use the expression map[key] = value
:
If the map already contains an element with that key, then the value will be overwritten.
To add a new element, the
m
variable must be declared with themut
modifier.
Delete elements from a map
To delete an element from a map, use the map.delete()
method:
To delete an element, the
m
variable must be declared with themut
modifier.
Get element from a map by key
To get an element from a map, use the map[key]
expression:
If the key is not found, it will return Zero-value for a value type:
To return a different value if the key is not found, or to add custom handling for this case,
use the or
block:
or
block, as in the case of
handling Result/Option types,
can contain several statements:
You can also check that a key is present in the map, and get its value if found, in a single
expression using if
unwrapping:
Check if a key exists in a map
To check if a key exists in a map, use the key in map
expression:
!in
can be used to check for the absence of a key in a map:
Get all keys from a map
To get all the keys from the map, use the map.keys()
method:
Get all values from a map
To get all values from a map, use the map.values()
method:
Check if a value exists in a map
To check if a value is in a map, you can use a combination of map.keys()
and the in
operator:
Unlike the operation of checking the presence of a key in the map, this operation requires a complete enumeration of all map values and has O(n) complexity.
Assigning one map to another
In V, you can't just assign the value of a map variable to another variable:
There are three ways to solve this problem:
Save a reference to the map in a variable
See References article for more information about how to use references.
In this case the
m2
variable will refer to the same map asm
, so changes in one map will be visible in the other.
clone()
method
Use the This method creates a copy of the map, so changes in one map will not be visible in another.
move()
method
Use the This method moves the map's internal data to a new map, thus not copying all the data, but moving
internal data pointers.
This is much more efficient than cloning the map, but after calling move()
the m
variable will
be an empty map.
Only mutable maps can be moved.