Vlang tutorials — hot code reload
Hey everyone, this is the second short tutorial on vlang, for the tutorials I try to pick issues that I faced while dealing with the language and present the solutions to them, and today's tutorial will be focused on hot code reloading.
I faced issues while following the hot code reload tutorial on V documentation and examples where I had to build an issue, but today (December 4th) I finally found the solution by reading other similar issues
So let's start with the basics, for this example, we are gonna declare a function that prints a message and on the main function we will call that function inside a loop every n milliseconds:
module main
import time
fn hot_code(v int) {
println('message to change $v')
}
fn main() {
mut v := 1
for {
hot_code(v)
time.sleep(500 * time.millisecond)
v+=1
}
}
if we run the example with:
v run hotcodereload.v
we should see the message being printed on the terminal, but if we change the content of the println nothing will happen, for it to work we have to add the live property on top of the hot_code function:
[live]
fn hot_code(v int) {
println('message to changes =>=> 3 $v')
}
But if we try to run with the command on the tutorial:
v -live run hotcodereload.v
We get an error of memory access, to avoid this error, we just have to use the watch command instead of the run like this:
v watch run hotcodereload.v
And now the code will run, and you can just change the message in the println and your changes will be loaded automatically
And it works!!!
It's incredible how a simple thing took me weeks to figure out, so I'm creating this tutorial, so you don't have to go through this too.
Hope you liked it and mostly hope that you find it useful
Thanks for reading!