What's wrong with this example #20239
-
From github Thanks in advance. Error message:
ex06.v import os
fn main() {
path := './data/file.txt'
text := 'Full text description.'
if contents := os.write_file(path, text) or {
println('Failed while creating file')
return
}
content_lines := read_file(path) // content_lines=read_file(path) changed = to :=
print(content_lines)
}
fn read_file(path string) {
contents := os.read_file(path.trim_space()) or {
println('Failed to open $path')
return
}
return contents.split_into_lines()
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The problem is that that example is 4 years old... badly outdated, compared to current V. The simplest working replacement is: import os
fn main() {
path := 'file.txt'
text := 'Full text description.'
os.write_file(path, text) or {
println('Failed while creating file')
return
}
content_lines := os.read_lines(path)!
println(content_lines)
} |
Beta Was this translation helpful? Give feedback.
-
import os
fn main() {
path := './data/file.txt'
text := 'Full text description.'
os.write_file(path, text) or {
println('Failed while creating file')
return
}
content_lines := read_file(path)
print(content_lines)
}
fn read_file(path string) []string {
contents := os.read_file(path.trim_space()) or {
println('Failed to open $path')
return []
}
return contents.split_into_lines()
} In my opinion the linked example never worked, but perhaps was just a draft. |
Beta Was this translation helpful? Give feedback.
The problem is that that example is 4 years old... badly outdated, compared to current V.
The simplest working replacement is: