Go has one associated data structure - “map”. Map is a hash
table where you can store data in key/value pairs. Following syntax is
used to create a map.
varxmap[keyType]valType
Type of the key is defined within [] and type of value is defined followed by it.
Variable x can hold key/value pair with key as type of string and value as type of int. The type of the key should be the type that is comparable such as int, string, or an array. But, slice is not allowed as slice is not comparable. The above example create a null map. Adding key/value in null map panic and throw error.
But, if you still want to check if the given key exists or not, then
it optionally return second argument that is boolean and tell if given
key is exists or not.
If you want to remove given key/value pair, delete() function is used. The first argument should be the map and second argument should be the key which need to be deleted.
You’re reading second article. Click here to read first article.
Arrays
Arrays in Go are fixed-size and contains values of the same type. To use an array, we must need to define both size and the type.
varx[n]type
Here, n is the size of the array x and type is one of the valid types in Go. For example:
varx[3]int
The x variable now can hold three integer values. By default, arrays are initialized with zero value of its type. So, the shape of the x should be [0 0 0].
Yes, we must need to specify the size and type on the right side as
well. If an array is declared with the initial value, we can omit the
size and type from the left side.
As array are fixed-size, there are only limited operations one can
do. For example, you can’t do push or pop or append element from/to
array. Due to this, most of the time, slices are used instead of an
array which we will cover in very next section. But, there is one more
function which we can use with an array and that is - cap() function for capacity to hold element in array. But, it returns the same values as len() return because of the fixed-size.
Slices
Slices are arrays but can grow or shrink as needed. Defining slice is
almost similar to array except due to grow and shrink capabilities, we
do not have to define the size of slice.
varx[]type
The syntax difference between array and slice is - empty bracket without size.
For example,
varx[]int
The underlying structure of the slice is a pointer to an array. Due to this, initial value of slice x is nil instead of zero value because memory is not allocated to this slice.
As slice is initialized with empty slice, both length and capacity is 0. But, we can add the element to the slice x using append() function. append() is a built-in function that can append an element to the slice. append() function accepts two arguments - slice and element that we want to append in the slice.
x=append(x,2)
Here, we are adding element 2 in slice and saving the result of append() which is slice into x.
But, if you want to make an independent slice from the original slice, we need to use copy() function. The copy()
function accepts two arguments - destination slice where elements will
be copied and source slice from which elements will be copied. That
means we first need to create destination slice with the same type and
size of the slice we want to copy from. We can use make() function to create a new slice with type and initial size. make() accepts two arguments - type and size. We can get the size of slice x using len(x). The value make() function return can be saved into the variable.