Created
November 3, 2023 22:32
-
-
Save davlgd/130af4147112f81cfa8135c5f84cee91 to your computer and use it in GitHub Desktop.
V recursive struct demo app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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