Skip to content

Instantly share code, notes, and snippets.

@davlgd
Created November 3, 2023 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davlgd/130af4147112f81cfa8135c5f84cee91 to your computer and use it in GitHub Desktop.
Save davlgd/130af4147112f81cfa8135c5f84cee91 to your computer and use it in GitHub Desktop.
V recursive struct demo app
module main
struct Directory {
name string
subs []Directory
files []string
}
// Prints the directory structure recursively
fn print_directory(dir Directory, level int) {
println("${' '.repeat(level)}$dir.name/")
for file in dir.files {
println("${' '.repeat(level + 1)}$file")
}
for subdir in dir.subs {
print_directory(subdir, level + 1)
}
}
// Creates a directory structure
fn main() {
root := Directory{
name: "",
subs: [
Directory{
name: "home",
files: ["todo.txt", "notes.txt", "about.md"],
},
Directory{
name: "var",
files: ["log.txt", "error.log", "debug.log"],
subs: [
Directory{
name: "www",
files: ["index.html", "about.html"],
},
Directory{
name: "lib",
files: ["lib1.so", "lib2.so"],
},
],
},
],
}
// Prints the directory structure
print_directory(root, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment