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.
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)
Go to cmd, type command:
go env -w GO111MODULE=off
Finally, use command:
go run ./
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With