V for Go programmers — Types
Still following the structure of the book “Introducing Go”, the second chapter is about types, V is also a statically typed language.
Numbers
V has different ways to represent numbers into many categories:
Number
/ \
Integers Floats
/ \
i8 i16 f32 f64
int i64
u8 u16
u32 u64
Integers
Like Go, V separates signed integers from unsigned ones by the u character(u8, u16, u32, u64), to distinguish between numbers that can be negative or not, also notice that integers in V don’t have an i32 type, instead, there is an int type, because in V a default int type will always be an i32.
Floats
And for the floats, we just have two distinctions, an f32 and f64 to represent decimal numbers with more or less precision.
Example
Let’s write an example program using numbers.
println("1 + 1 = = ${1+1}")
V also supports other mathematical operators like / for division, * for multiplication, — for subtraction, % for the remainder, and other useful functions in the math module.
Strings
Like Go, strings in V are also represented by double-quoted characters:
"this is a string"
"this is the length".len // outputs the length of a string
"this is the first byte of the string"[0] // outputs the byte 116
"the first character"[0].ascii_str() //outputs `t`
Runes
In the last example you may have noticed that the output wasn’t a string it was quoted with: ``
Because the output wasn’t a string at all it was a rune a type that stores only one character, similar to the type char in many other languages:
`🚀` // rune
`🚀`.str() // converts rune to string
`🚀`.bytes() // return a list of bytes
Booleans
The booleans equivalent in V are true and false:
true
false
true && true
false || true
Other types
V also has other less used types like isize and usize, that are platform-dependent, the size is how many bytes it takes to reference any location in memory
And voidptr which is most commonly used to interface with C.
And these are the types in V, hope it was useful to you, I don’t yet how many episodes I will write in this series, probably as much as I get tired of writing
Thank you for reading!