'Go ReadString issues: The filename, directory name, or volume label syntax is incorrect
can anyone figure this out? I think the problem might be in the readstring() argument when being passed to the create file function. I tried resolving it by stripping '\n' but the error remains. Is there a workaround other than readstring()? Because I think it's the one causing issues. Thank you
package main
import (
"fmt"
"bufio"
"log"
"strings"
//"io/ioutil"
"os"
)
func main() {
fmt.Print("Enter filename to create: ")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
text = strings.TrimSuffix(text, "\n")
if len(text) > 3 {
//newFile, err := os.Create(text)
fmt.Println("Name:"+text)
createFile(text)
} else {
fmt.Println("invalid filename")
}
}
func createFile(fileName string){
var newFile *os.File
var err error
fmt.Println(fileName)
newFile, err = os.Create(fileName)
if err != nil {
log.Fatal(err)
}
newFile.Close()
}
Ok for those who are new to this problem, I've manage to find a workaround using scanf. So consider this problem solved.
Solution 1:[1]
Start with basic debugging techniques: check for errors and check input.
Replace
text, _ := reader.ReadString('\n')
with
text, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
fmt.Printf("%q\n", text)
For example,
so1.go
:
package main
import (
"bufio"
"fmt"
"log"
"strings"
//"io/ioutil"
"os"
)
func main() {
fmt.Print("Enter filename to create: ")
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
fmt.Printf("%q\n", text)
text = strings.TrimSuffix(text, "\n")
if len(text) > 3 {
//newFile, err := os.Create(text)
fmt.Println("Name:" + text)
createFile(text)
} else {
fmt.Println("invalid filename")
}
}
func createFile(fileName string) {
var newFile *os.File
var err error
fmt.Println(fileName)
newFile, err = os.Create(fileName)
if err != nil {
log.Fatal(err)
}
newFile.Close()
}
Output:
>go run so1.go
Enter filename to create: test.file
"test.file\r\n"
Name:test.file
test.file
: The filename, directory name, or volume label syntax is incorrect.
exit status 1
On Windows, the line is terminated with "\r\n
".
Replace
text = strings.TrimSuffix(text, "\n")
with
text = strings.TrimSuffix(text, "\n")
text = strings.TrimSuffix(text, "\r")
For example,
so2.go
:
package main
import (
"bufio"
"fmt"
"log"
"strings"
//"io/ioutil"
"os"
)
func main() {
fmt.Print("Enter filename to create: ")
reader := bufio.NewReader(os.Stdin)
text, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
fmt.Printf("%q\n", text)
text = strings.TrimSuffix(text, "\n")
text = strings.TrimSuffix(text, "\r")
if len(text) > 3 {
//newFile, err := os.Create(text)
fmt.Println("Name:" + text)
createFile(text)
} else {
fmt.Println("invalid filename")
}
}
func createFile(fileName string) {
var newFile *os.File
var err error
fmt.Println(fileName)
newFile, err = os.Create(fileName)
if err != nil {
log.Fatal(err)
}
newFile.Close()
}
Output:
>go run so2.go
Enter filename to create: test.file
"test.file\r\n"
Name:test.file
test.file
Now that the program works, remove this debugging statement:
fmt.Printf("%q\n", text)
Solution 2:[2]
Go to cmd, type command:
go env -w GO111MODULE=off
Finally, use command:
go run ./
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | peterSO |
Solution 2 | Johto Robbie |