V for Go programmers — Arrays, and Maps

Kevin Da Silva
2 min readApr 18, 2023

Covering one more topic in Vlang based on the chapters of the book “Introducing Go”, and following the order of the book, today’s is the fifth chapter, “Arrays, Slices, and Maps” respectively.

Arrays

Arrays and slices are pretty much the same thing in V, considering V groups the two of them:

mut arr := []int
arr << 3 // [3]
arr << 2 // [3,2]
arr.len // 2
arr[2] = 5

And fixed size arrays:

mut v := [3]int{}
v[2] = 13

println(v) // [0,0,13]

Arrays in V also can be created with a default value:

v := []int{len: 3, init: 3}

println(v) // [3, 3, 3]

And also I can use the “it” keyword to populate the array:

v := []int{len: 3, init: it+1}

println(v) // [1, 2, 3]

“it” represents the current index being populated starting at 0, with the facility of the “it” keyword we can for example revert an array:

v := []int{len: 3, init: it+1}
println(v) // [1, 2, 3]

v2 := []int{len: v.len, init: v[v.len-it-1]}
println(v2) // [3, 2, 1]

Now more properly getting into slices:

nums := [0, 10, 20, 30, 40]
println(nums[1..4]) // [10, 20, 30]
println(nums[..4]) // [0, 10, 20, 30]
println(nums[1..]) // [10, 20, 30, 40]
array_1 := [3, 5, 4, 7, 6]
mut array_2 := [0, 1]
array_2 << array_1[..3]
println(array_2) // [0, 1, 3, 5, 4]

V even supports negative indexes:

a := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
println(a#[-3..]) // [7, 8, 9]

Maps

Maps work pretty much the same as in Go, you defined the type of your key and the type of your value:

mut m := map[string]int{} 
m['one'] = 1
m['two'] = 2
println(m['one']) // 1

Or an immutable map:

numbers := {
'one': 1
'two': 2
}
println(numbers)

Exercises

  • Write a program that finds the smallest number in this list: x := [ 48,96,86,68, 57,82,63,70, 37,34,83,27, 19,97, 9,17]

<- CHAPTER 3,4 | CHAPTER 6 ->

And that’s how you work with arrays and maps in V.

Thank you for reading one more chapter in this series on V, hope to see you in the next one!

--

--

Kevin Da Silva

I'm a back-end developer, functional programming lover, fascinated by computer science and languages. From the south part of Brazil to the world