vlang hot code reloading

1-12 585 views

https://kevin-da-silva.medium.com/vlang-tutorials-hot-code-reload-bfad7186a08d

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!

欢迎留言