I want to read contents of text file.
When I am passing the file name as string like this:
stream, err = ioutil.ReadFile("sample.txt")
its working. Its even working if do in this way:
filename := "sample.txt"
stream, err = ioutil.ReadFile(filename)
But when I get the value of filename from string array, it fails to get the file and throw the error: The filename, directory name, or volume label syntax is incorrect
filename := lines[1] //where lines[] is an array of strings
stream, err = ioutil.ReadFile(filename)
fmt.Printf("%q
", lines[1]) // output: mytext2.txt
The application should trim the from the end of the string using
strings.TrimSuffix(filename, "")
or strings.TrimSpace(filename)
.
If OP used strings.Split(s, " ", -1)
to create lines
, then the trailing can also be avoided by splitting on
" "
instead.